2012-03-15 38 views
2

我想写一个扩展到Visual Studio,这将使我能够为指定的表生成一个模型。如何从服务器资源管理器检索连接字符串

我用下面的代码mycommand的项目加入到表的上下文菜单中的服务器资源管理器:

Commands2 commands = (Commands2)_applicationObject.Commands; 
CommandBar menuBarCommandBar = ((CommandBars)_applicationObject.CommandBars)["Object Node"]; 

Command command = commands.AddNamedCommand2(_addInInstance, "MyCommand", "MyCommand", 
    "Executes the command for MyCommand", true, 59, ref contextGUIDS, 
    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, 
    (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); 
if ((command != null) && (menuBarCommandBar != null)) 
{ 
     command.AddControl(menuBarCommandBar, 1); 
} 

获取所选表项目的名称:

string fileName = "Dafault.cs"; 
var serverExplorer = _applicationObject.ToolWindows.GetToolWindow("Server Explorer") as UIHierarchy; 
if (serverExplorer != null) 
{ 
    dynamic item = ((object[])serverExplorer.SelectedItems)[0]; 
    fileName = string.Format("{0}.cs", item.Name); 
} 

//... 
// Generate model based on table from database 
//... 

_applicationObject.ItemOperations.NewFile("General\\Text File", fileName, Constants.vsViewKindCode); 

我怎样才能获取有关数据库连接的信息?

回答

0

Brad Larson,为什么我的问题被删除?

找到解决方案。 使用this

 
public static IDbConnection GetConnection(DSRefNavigator navigator, out string type) 
     { 
      type = null; 
      try 
      { 
       if (navigator != null) 
       { 
        IVsDataConnectionsService dataConnectionsService = 
         (IVsDataConnectionsService) Package.GetGlobalService(typeof(IVsDataConnectionsService)); 
        string itemName = navigator.GetConnectionName();

if (itemName != null) { int iConn; // = dataConnectionsService.GetConnectionIndex(itemName); DataViewHierarchyAccessor dataViewHierarchy = null; for(iConn = 0; iConn < dataConnectionsService.Count; iConn++) { DataViewHierarchyAccessor hierarchyAccessor = new DataViewHierarchyAccessor((IVsUIHierarchy) dataConnectionsService.GetConnectionHierarchy(iConn)); try { if (hierarchyAccessor.Connection.DisplayConnectionString == itemName) { dataViewHierarchy = hierarchyAccessor; break; } } catch { } } if (dataViewHierarchy != null) { DataConnection connection = dataViewHierarchy.Connection; if (connection != null && connection.ConnectionSupport.ProviderObject != null) { type = connection.ConnectionSupport.ProviderObject.GetType().FullName; return (IDbConnection) connection.ConnectionSupport.ProviderObject; } } } } } catch { } return null; }

+0

_why我的问题是deleted_因为你张贴作为一个答案;-) – kleopatra 2012-11-11 11:23:24

+0

感谢。很明显,但我找不到问题发表评论的方法(它只能用于答案)。一定是我困惑的东西。 – 2012-11-12 06:15:45

+1

要走的路是问你自己的问题,可能与此链接(显示搜索努力 - 就像你做的一样)总是一个好兆头。你可以回答和评论你自己的问题,所以可能试图吸引在你的问题的评论中,以前的海报与@username。虽然从未尝试过。欢呼声,欢迎来到SO :-) – kleopatra 2012-11-12 10:28:03

相关问题