Logo
HOWTO: Get data sources, data providers and data connections from a Visual Studio 2005 add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: March 2007    

Introduction

Visual Studio 2005 introduces the Data Designer Extensibility (DDEX) SDK, which provides design-time capabilities for .NET 2.0 Data Providers. This article explains how to get the .NET data sources and .NET DDEX data providers installed on your computer, and how to get the data connections defined in the Server Explorer of Visual Studio 2005. 

More Information

To get the data-related information from a Visual Studio 2005 add-in you need to use several services provided by the DTE object from the DDEX SDK. You need the Microsoft.VisualStudio.Data.Interop.dll assembly located in the folder C:\Program Files\Microsoft Visual Studio 8\Common7\IDE. Also, see at the end of this article how to get a service from the Visual Studio IDE from an add-in.

The following procedure displays information about the data providers, data sources and data connections:

Imports Microsoft.VisualStudio.Data.Interop
Public Sub GetDataInformation(ByVal objDTE As EnvDTE.DTE)
   Dim objIVsDataProviderManager As IVsDataProviderManager
   Dim objIVsDataProvider As IVsDataProvider
   Dim objIVsDataSourceManager As IVsDataSourceManager
   Dim objIVsDataSource As IVsDataSource
   Dim objIVsDataConnectionsService As IVsDataConnectionsService
   Dim objIVsDataConnectionManager As IVsDataConnectionManager
   Dim objIVsDataConnection As IVsDataConnection
   Dim sMsg As String
   Dim objProviderGuid As Guid
   Dim iConnectionIndex As Integer
   Dim sConnectionName As String
   Dim sConnectionString As String
   objIVsDataProviderManager = CType(GetService(objDTE, GetType(IVsDataProviderManager)), IVsDataProviderManager)
   objIVsDataSourceManager = CType(GetService(objDTE, GetType(IVsDataSourceManager)), IVsDataSourceManager)
   objIVsDataConnectionsService = CType(GetService(objDTE, GetType(IVsDataConnectionsService)), IVsDataConnectionsService)
   objIVsDataConnectionManager = CType(GetService(objDTE, GetType(IVsDataConnectionManager)), IVsDataConnectionManager)
   ' Data providers
   For Each objIVsDataProvider In objIVsDataProviderManager.GetDataProviders
      sMsg = "Data Provider: " & objIVsDataProvider.DisplayName & Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= "Description: " & objIVsDataProvider.Description & Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= "Guid: " & objIVsDataProvider.Guid.ToString & Microsoft.VisualBasic.ControlChars.CrLf
      System.Windows.Forms.MessageBox.Show(sMsg)
   Next
   ' Data sources
   For Each objIVsDataSource In objIVsDataSourceManager.GetDataSources()
      sMsg = "Data Source: " & objIVsDataSource.DisplayName & Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= "Guid: " & objIVsDataSource.Guid.ToString & Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= "Providers: " & Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= Microsoft.VisualBasic.ControlChars.CrLf
      For Each objProviderGuid In objIVsDataSource.GetProviders()
         objIVsDataProvider = objIVsDataProviderManager.GetDataProvider(objProviderGuid)
         sMsg &= objIVsDataProvider.DisplayName & Microsoft.VisualBasic.ControlChars.CrLf
      Next
      System.Windows.Forms.MessageBox.Show(sMsg)
   Next
   ' Data connections
   For iConnectionIndex = 0 To objIVsDataConnectionsService.Count - 1
      sConnectionName = objIVsDataConnectionsService.GetConnectionName(iConnectionIndex)
      objProviderGuid = objIVsDataConnectionsService.GetProvider(iConnectionIndex)
      sMsg = "Data Connection: " & sConnectionName & Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= Microsoft.VisualBasic.ControlChars.CrLf
      objIVsDataProvider = objIVsDataProviderManager.GetDataProvider(objProviderGuid)
      sMsg &= "Provider: " & objIVsDataProvider.DisplayName & Microsoft.VisualBasic.ControlChars.CrLf
      sMsg &= Microsoft.VisualBasic.ControlChars.CrLf
      sConnectionString = objIVsDataConnectionsService.GetConnectionString(iConnectionIndex)
      objIVsDataConnection = objIVsDataConnectionManager.GetDataConnection(objProviderGuid, sConnectionString, True)
      sMsg &= "Connection String: " & objIVsDataConnection.DisplayConnectionString & Microsoft.VisualBasic.ControlChars.CrLf
      System.Windows.Forms.MessageBox.Show(sMsg)
   Next
End Sub
Public Function GetService(ByVal serviceProvider As Object, ByVal type As System.Type) As Object
   Return GetService(serviceProvider, type.GUID)
End Function
Public Function GetService(ByVal serviceProvider As Object, ByVal guid As System.Guid) As Object
   Dim objService As Object = Nothing
   Dim objIServiceProvider As Microsoft.VisualStudio.OLE.Interop.IServiceProvider
   Dim objIntPtr As IntPtr
   Dim hr As Integer
   Dim objSIDGuid As Guid
   Dim objIIDGuid As Guid
   objSIDGuid = guid
   objIIDGuid = objSIDGuid
   objIServiceProvider = CType(serviceProvider, Microsoft.VisualStudio.OLE.Interop.IServiceProvider)
   hr = objIServiceProvider.QueryService(objSIDGuid, objIIDGuid, objIntPtr)
   If hr <> 0 Then
      System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr)
   ElseIf Not objIntPtr.Equals(IntPtr.Zero) Then
      objService = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(objIntPtr)
      System.Runtime.InteropServices.Marshal.Release(objIntPtr)
   End If
   Return objService
End Function

Related articles

 



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top