locked
How to get only MS Sql Server connections from Server Explorer? RRS feed

  • Question

  • I'm using IVsDataConnectionManager to return all connections from Server Explorer.

    Everything works fine, but I have one question: how to check that the connection is for ms sql server?

    I need only sql server connections.

    Denis
    Tuesday, June 1, 2010 3:03 PM

Answers

  • Hi, Denis

    Because every DataProvider has its own guid, so all we need to do is to get the connection GUID.

    All the Guid is in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\DataProviders.

    So to find what connection is owned by sql server, please see the following code.

                Guid SqlClientProviderGuid = new Guid("91510608-8809-4020-8897-fba057e22d54");

                IVsDataExplorerConnectionManager connectionManager = GetService(typeof(IVsDataExplorerConnectionManager)) as IVsDataExplorerConnectionManager;

                System.Collections.Generic.IDictionary<string, IVsDataExplorerConnection> connections = connectionManager.Connections;

                foreach (KeyValuePair<string, IVsDataExplorerConnection> connection in connections)

                {

                    //

                    if (SqlClientProviderGuid.Equals(connection.Value.Provider))

                    {

                        //Sql server client

                    }

     

                    string NodeText = connection.Value.DisplayName;

                }

    Thanks

    Chao

    • Marked as answer by Chao Kuo Tuesday, June 8, 2010 2:42 AM
    Wednesday, June 2, 2010 9:31 AM
  • Thank you.

    I don't like guids in my code.

    I found another solution:

    var list = new List();
    
    var _Service = GetService(typeof(IVsDataConnectionsService))
    						as IVsDataConnectionsService;
    
    for (int i = 0; i < _Service.Count; i++)
    {
    	var dataViewHierarchy =
    				new DataViewHierarchyAccessor(_Service.GetConnectionHierarchy(i) 
    								as IVsUIHierarchy);
    
    	var connection = dataViewHierarchy.Connection;
    
    	if (connection.ConnectionSupport.ProviderObject is SqlConnection)
    	{
    		list.Add(new KeyValuePair<string, string>(_Service.GetConnectionName(i),
    													connection.ConnectionSupport.ConnectionString));
    	}
    }

     

    • Marked as answer by abap Wednesday, June 2, 2010 5:50 PM
    • Unmarked as answer by abap Wednesday, June 2, 2010 5:51 PM
    • Marked as answer by Chao Kuo Tuesday, June 8, 2010 2:42 AM
    Wednesday, June 2, 2010 1:39 PM

All replies

  • Hi, Denis

    Because every DataProvider has its own guid, so all we need to do is to get the connection GUID.

    All the Guid is in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\DataProviders.

    So to find what connection is owned by sql server, please see the following code.

                Guid SqlClientProviderGuid = new Guid("91510608-8809-4020-8897-fba057e22d54");

                IVsDataExplorerConnectionManager connectionManager = GetService(typeof(IVsDataExplorerConnectionManager)) as IVsDataExplorerConnectionManager;

                System.Collections.Generic.IDictionary<string, IVsDataExplorerConnection> connections = connectionManager.Connections;

                foreach (KeyValuePair<string, IVsDataExplorerConnection> connection in connections)

                {

                    //

                    if (SqlClientProviderGuid.Equals(connection.Value.Provider))

                    {

                        //Sql server client

                    }

     

                    string NodeText = connection.Value.DisplayName;

                }

    Thanks

    Chao

    • Marked as answer by Chao Kuo Tuesday, June 8, 2010 2:42 AM
    Wednesday, June 2, 2010 9:31 AM
  • Thank you.

    I don't like guids in my code.

    I found another solution:

    var list = new List();
    
    var _Service = GetService(typeof(IVsDataConnectionsService))
    						as IVsDataConnectionsService;
    
    for (int i = 0; i < _Service.Count; i++)
    {
    	var dataViewHierarchy =
    				new DataViewHierarchyAccessor(_Service.GetConnectionHierarchy(i) 
    								as IVsUIHierarchy);
    
    	var connection = dataViewHierarchy.Connection;
    
    	if (connection.ConnectionSupport.ProviderObject is SqlConnection)
    	{
    		list.Add(new KeyValuePair<string, string>(_Service.GetConnectionName(i),
    													connection.ConnectionSupport.ConnectionString));
    	}
    }

     

    • Marked as answer by abap Wednesday, June 2, 2010 5:50 PM
    • Unmarked as answer by abap Wednesday, June 2, 2010 5:51 PM
    • Marked as answer by Chao Kuo Tuesday, June 8, 2010 2:42 AM
    Wednesday, June 2, 2010 1:39 PM