Tuesday, August 10, 2010

Invoking the ConnectionString Editor in Visual Basic.NET application

image  image

Add project references to:

  • adodb.dll
    NET: ADODB - C:\Program Files\Microsoft.NET\Primary Interop Assemblies\adodb.dll
  • MSDASC.dll
    COM: Microsoft OLE DB Service Component 1.0 Type Library

Sample code to invoke ConnectionString editor to create a new connection string or edit an existing one:

   1:  Imports MSDASC
   2:  Imports ADODB
   3:   
   4:  Public Class FormSettings
   5:   
   6:      Private Sub ButtonEditConnectionString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEditConnectionString.Click
   7:   
   8:          Dim szConnectionString As String = TextBoxConnectionString.Text.Trim()
   9:   
  10:          Dim I As MSDASC.DataLinks = New DataLinks
  11:          Dim C As ADODB.Connection = New ADODB.Connection
  12:   
  13:          If szConnectionString.Length() = 0 Then
  14:              If I.PromptNew(C) Then
  15:                  TextBoxConnectionString.Text = C.ConnectionString
  16:              End If
  17:          Else
  18:              C.ConnectionString = szConnectionString
  19:              If I.PromptEdit(C) Then
  20:                  TextBoxConnectionString.Text = C.ConnectionString
  21:              End If
  22:          End If
  23:   
  24:      End Sub
  25:   
  26:      Private Sub FormSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  27:   
  28:          'Connection String
  29:          TextBoxConnectionString.Text = My.Settings.ConnectionString
  30:   
  31:      End Sub
  32:   
  33:  End Class

No comments: