1

i want to develop a Visual Studio Extension.

Therefore i need all Databases that are connected via Visual Studio Sql Server Object Explorer. I want to connect to some of them during my programm execution.

I already tried to get all connected databases via

System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

But the result is not the right set of databases.

If its possible to get all the information about the connected Databases my program should like:

var databases = GetAllConnectedDatabases();
foreach (database in databases)
{
   database.Connect(); 
   // do something
}

Is it possible to get information about all Sql-Servers i connected via Visual Studio Sql Server Object Explorer?

Thanks for your help.

dertrautmann
  • 63
  • 1
  • 10

1 Answers1

1

We could retrieve related connections via IVsDataExplorerConnectionManager Interface. Here is a simple demo for your reference.

 IVsDataExplorerConnectionManager decMgr = (IVsDataExplorerConnectionManager)this.ServiceProvider.GetService(typeof(IVsDataExplorerConnectionManager));

 foreach (var conn in decMgr.Connections)
 {
      var state = conn.Value.Connection.State;
      if (state == DataConnectionState.Closed)
      {
          conn.Value.Connection.Open();
      }
 }

enter image description here

Zhanglong Wu - MSFT
  • 1,652
  • 1
  • 7
  • 8
  • Thank you. But.. I am always getting 0 connections. – dertrautmann Apr 03 '17 at 14:13
  • Please check if you have Data Connections on your Server Explorer. – Zhanglong Wu - MSFT Apr 04 '17 at 02:55
  • I ensured, that the server / database is connected. But now i get an Build-Error The “GetDeploymentPathFromVsixManifest” task failed unexpectedly : (http://stackoverflow.com/questions/42985554/the-getdeploymentpathfromvsixmanifest-task-failed-unexpectedly) ... No solution found yet, so i cant test at the moment. But thank you very much for your input. – dertrautmann Apr 04 '17 at 05:57
  • Able to build again, still 0 connections in the IVsDataExplorerConnectionManager.Connections. My databases are listed as connected in the Server-Explorer. – dertrautmann Apr 04 '17 at 07:22
  • what kind of visual studio version you are using, vs2017 or others? My demo based on vs 2015 enterprise, it works. – Zhanglong Wu - MSFT Apr 04 '17 at 07:44
  • I am using Microsoft Visual Studio Enterprise 2017 Version 15.0.26228.10 D15RTWSVC Microsoft .NET Framework Version 4.6.01590 – dertrautmann Apr 04 '17 at 08:29
  • 1
    @ColeWu-MSFT the user is asking how to get the connections inside the "SQL Server Object Explorer" window, not from "Server Explorer" Window. Is there any possibility? I also need the answer – Kiran B May 17 '17 at 17:24