Freigeben über


Dr. eX: How can I programmatically add a database connection to the server explorer toolwindow with my add-in?

For VS 2008, you can use the IVsDataExplorerConnectionManager.AddConnection method.

The easiest way to retrieve this interface is with the help of the Visual Studio SDK. You’ll need to install the VS SDK and add the following references to your add-in project.

  • Microsoft.VisualStudio.OLE.Interop.dll

  • Microsoft.VisualStudio.Shell.9.0.dll

  • Microsoft.VisualStudio.Data.dll

  • Microsoft.VisualStudio.Data.Core.dll

  • Microsoft.VisualStudio.Data.Framework.dll

  • Microsoft.VisualStudio.Data.Services.dll

Add the following using statements to your connect.cs file:
 

using Microsoft.VisualStudio.Shell;

using Microsoft.VisualStudio.Data.Core;

using Microsoft.VisualStudio.Data.Services;

using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

The you can retrieve and use the IVsDataExplorerConnectionManager.AddConnection method as follows:
 

// Quick test to connect to my SQL Server Northwind DB.

private void Test()

{

// Create a ServiceProvider object off the DTE object.

ServiceProvider sp = new ServiceProvider((IOleServiceProvider)_applicationObject);

// Retrieve the IVsDataExplorerConnectionManager service

IVsDataExplorerConnectionManager decMgr = (IVsDataExplorerConnectionManager)sp.GetService(typeof(IVsDataExplorerConnectionManager).GUID);

// This is the guid that identifies the SQL Server Provider (see HKLM\SOFTWARE\Microsoft\VisualStudio\9.0\DataProviders

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

// Add a connection node to the server explorer

decMgr.AddConnection("My Northwind DB", guidProvider, "Data Source=MyServer;Initial Catalog=Northwind;Integrated Security=True", false);

}

For those of you still using Visual Studio 2005, you’ll have to use an undocumented class called Microsoft.VisualStudio.Data.DataExplorerConnectionManager.

Again the easiest way to retrieve this class is to use the Visual Studio SDK (version 4 for VS 2005).

You’ll need to add the following references to your add-in project: 

  • Microsoft.VisualStudio.Shell.dll

  • Microsoft.VisualStudio.OLE.Interop.dll

  • Microsoft.VisualStudio.Data.dll 

The following using statements to your connect.cs file:
 

using Microsoft.VisualStudio.Shell;

using Microsoft.VisualStudio.Data;

using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

Then retrieve the DataExplorerConnctionManager and invoke it’s AddConnection method similar to the following:
 

private void Test()

{

// Create a ServiceProvider object from the DTE object

Microsoft.VisualStudio.Shell.ServiceProvider sp = new Microsoft.VisualStudio.Shell.ServiceProvider((IOleServiceProvider)_applicationObject);

// Retrieve the DataExplorerConnectionManager Service

DataExplorerConnectionManager dceMgr = (DataExplorerConnectionManager)sp.GetService(typeof(DataExplorerConnectionManager));

// This is the guid that identifies the SQL Server Provider (see HKLM\SOFTWARE\Microsoft\VisualStudio\8.0\DataProviders

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

// Add a connection node to the server explorer

dceMgr.AddConnection("My Northwind DB", guidProvider, "Data Source=MyServer;Initial Catalog=Northwind;Integrated Security=True", false);

}

Happy coding,
Dr. eX