Making Stuff Faster
Design, Code, Database Performance

Connection String generation Visual Studio macro

Wednesday, October 26, 2005 6:50 AM

Speaking of usefull macros, this is one I've used for a a few years and thought might be usefull.  It calls the ADODB Data Link Properties dialog (shown below).  After you go through the dialog picking your data source, database, and connection settings, it dumps out the connection string for your configuration into code window where your cursor is located.

   

Here is the code for the connection string macro:

Public Sub ConnectionStringWizard()

     Dim args() As Object

     Dim linkType As System.Type = System.Type.GetTypeFromProgID("DataLinks")

     Dim linkObj As Object = Activator.CreateInstance(linkType)

     Dim conObj = linkType.InvokeMember("PromptNew", System.Reflection.BindingFlags.InvokeMethod, Nothing, linkObj, args)

     If conObj Is Nothing = False Then

          Dim constring As String = conObj.GetType().InvokeMember("ConnectionString", System.Reflection.BindingFlags.GetProperty, Nothing, conObj, args).ToString()

          Dim textSelection As TextSelection = DTE.ActiveDocument.Selection()

          Dim edit As EditPoint = textSelection.TopPoint.CreateEditPoint()

          edit.Insert("""" & constring & """")

     End If

End Sub


Feedback

# re: Connection String generation Visual Studio macro

Great idea!

Required an "Imports System" or qualifying Type and Activator as System.Type and System.Activator for me, by the way. 10/27/2005 1:29 PM | Jon Galloway

# re: Connection String generation Visual Studio macro

great idea, indeed!

thats the beauty of implicit import statements, btw :) 10/28/2005 4:27 AM | dominck

# re: Connection String generation Visual Studio macro

The conType variable is not needed.
You can delete this line:
Dim conType As Type = System.Type.GetTypeFromProgID("ADODB.Connection")

and you can change this line:
Dim conObj As Object = Activator.CreateInstance(conType)
to this:
Dim conObj = Nothing

conObj is being reset by the linkType.InvokeMember call. 10/28/2005 4:57 AM | Al Gonzalez

# re: Connection String generation Visual Studio macro

Cool macro, thanx... I'll use this one a lot... 10/30/2005 6:59 PM | Maruis Marais

Post a comment