1

有关节点的信息我已经建立了一个Visual Studio扩展,它增加了一个菜单项的“表”节点的上下文菜单,并在服务器探索者数据连接部分中的每个子节点。的Visual Studio扩展,让服务器资源管理器

enter image description here

所以菜单前BeforeQueryStatus事件触发出现这样我就可以动态地控制菜单项的状态,我有线了菜单项,使其启用/禁用或是否选择该菜单项应根本不出现。

在这两种BeforeQueryStatus事件和调用处理程序我需要访问关于所选节点表示的对象的信息。我需要知道在“Tables”父节点中表示的实际数据连接的Connection String属性。

Data connection properties

如果选择的节点是我还需要知道表名和模式的节点代表(“表”的孩子)的表节点。

我试图寻找不同的对象,似乎无法找到我后。任何帮助赞赏。这里是我的出发点:

private void Mnu_BeforeQueryStatus(object sender, EventArgs e) 
{ 
    OleMenuCommand mnu = (OleMenuCommand)sender; 
    EnvDTE.DTE service = (EnvDTE.DTE)this.ServiceProvider.GetService(typeof(EnvDTE.DTE)); 

    EnvDTE.ContextAttributes ctx = service.ContextAttributes; 
    EnvDTE.Window w = service.ActiveWindow; 
    //EnvDTE.Properties p = service.Properties.; 
    EnvDTE.SelectedItems si = service.SelectedItems; 

    EnvDTE.ProjectItem pi = w.ProjectItem; //null 
    var selection = w.Selection; //null 

    var obj = w.Object; 
    var objk = w.ObjectKind; 
} 

回答

1

你可以通过IVsDataExplorerConnectionManager和下面的代码获取所有连接以供参考。

IVsDataExplorerConnectionManager connectionManager = this.ServiceProvider.GetService(typeof(IVsDataExplorerConnectionManager)) as IVsDataExplorerConnectionManager; 
      IDictionary<string, IVsDataExplorerConnection> connections = connectionManager.Connections; 
      foreach (KeyValuePair<string, IVsDataExplorerConnection> connection in connections) 
      { 

       if(connection.Key == "youconnectionkey") 
       { 
        string connstr = connection.Value.Connection.DisplayConnectionString; 
        string NodeText = connection.Value.DisplayName; 
       using (SqlConnection conn = new SqlConnection(connstr)) 
       { 
        conn.Open(); 
        DataTable schema = conn.GetSchema("Tables"); 
        List<string> TableNames = new List<string>(); 
        foreach (DataRow row in schema.Rows) 
        { 
         TableNames.Add(row[2].ToString()); 
        } 
       } 


       } 
      } 

另外,请添加以下参考。

using Microsoft.VisualStudio.Data.Services; 
+0

这是伟大的!我想知道如何将连接管理器中的给定连接与服务器资源管理器中的节点相关联。由于我的事件在用户显示弹出式菜单时触发,所以我需要找到与用户右键点击的节点相关的连接。 – Jeremy

+0

你可以使用UIHierarchy.SelectedItems获取连接信息用户权限点击。相似的代码,你可以参考:https://stackoverflow.com/questions/15541438/how-to-get-the-selected-connection-node-object-of-vs-server-explorer-window-ddex –

相关问题