March 9, 2021
Hot Topics:

Implementing a Custom ConnectionString Installer for Setup

  • By Paul Kimmel
  • Send Email »
  • More Articles »

Figure 5: The Data Link Properties editor that you will use to configure the connection string at install time.

Listing 2: The installer's code that you need to write.

Option Strict On
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.Configuration
Imports System.IO
Imports MSDASC
Imports ADODB
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Diagnostics

Public Class Installer1

      Public Sub New()

         'This call is required by the Component Designer.

         'Add initialization code after the call to

      End Sub

   Public Overrides Sub Install( _
      ByVal stateSaver As System.Collections.IDictionary)


      Dim instance As DataLinksClass = New DataLinksClass()
      Dim connection As ConnectionClass = New ConnectionClass
      Dim o As Object = connection
      If (instance.PromptEdit(o) = True) Then

         Dim targetDirectory As String = Context.Parameters("targetDir")
         If (targetDirectory Is Nothing) Then
            Throw New InstallException("Target directory not specified")
         End If

         Dim parmConfigFile As String = Context.Parameters("configFile")

         ' what is target config file name
         Dim configFile As String = targetDirectory + parmConfigFile


         If (File.Exists(configFile) = False) Then
            Throw New InstallException( _
               String.Format("Configuration file {0} not found", _
         End If

         Dim config As Configuration = _
         Dim settings As ConnectionStringSettings = _
            New ConnectionStringSettings
         settings.Name = _
         Dim connectionToUse As String = _
         settings.ConnectionString = connectionToUse

         ' Encrypt the connection strings section
         Dim section As ConfigurationSection = _
         If ((section Is Nothing = False) And _
            (section.IsReadOnly() = False)) Then
            section.SectionInformation.ProtectSection _
            section.SectionInformation.ForceSave = True
         End If
      End If
   End Sub

   Function GetAdjustedConnectionString(ByVal connectionString As String) _
      As String
      Dim parts() As String = _
      Dim builder As StringBuilder = New StringBuilder()
      For Each part As String In parts

         If (Regex.IsMatch(part, "provider", RegexOptions.Compiled _
            Or RegexOptions.IgnoreCase) = False) Then
            builder.AppendFormat("{0};", part)
         End If
      Return builder.ToString
   End Function
End Class

The salient aspects of the code in Listing 2 are explained in the sub-sections that follow.

Page 3 of 4

This article was originally published on October 10, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date