July 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Implementing a Custom ConnectionString Installer for Setup

  • October 10, 2007
  • 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()
         MyBase.New()

         'This call is required by the Component Designer.
         InitializeComponent()

         'Add initialization code after the call to
         'InitializeComponent

      End Sub


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

      System.Diagnostics.Debugger.Break()

      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

         Debug.WriteLine(configFile)

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

         Dim config As Configuration = _
            ConfigurationManager.OpenExeConfiguration(configFile)
         Dim settings As ConnectionStringSettings = _
            New ConnectionStringSettings
         settings.Name = _
            "UsesConnection.My.MySettings.MyConnectionString"
         Dim connectionToUse As String = _
            GetAdjustedConnectionString(connection.ConnectionString)
         settings.ConnectionString = connectionToUse
         config.ConnectionStrings.ConnectionStrings.Clear()
         config.ConnectionStrings.ConnectionStrings.Add(settings)

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

   Function GetAdjustedConnectionString(ByVal connectionString As String) _
      As String
      Dim parts() As String = _
         connectionString.Split(Convert.ToChar(";"))
      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
      Next
      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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel