Visual Studio 2005 now leverages the strongly typed settings file which wraps app.config. With connection strings stored in app.config/Settings, developers can centralize their connection strings across their application.Some advantages over VS2003 are:
Customizing the connection string at runtimeDevelopers often want to customize the connection string. In most cases the password simply needs to be provided. The strongly typed Settings file also has a few events that can be leveraged.
Here’s a snippet I user for SQL Mobile to replace .\ with the data directory when running under click once. This same code can be modified to replace the password of a connection string.
Private Sub Settings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
Dim dataDirectory As String
' When running under debug mode, use the data file from the same directory as the executable
' otherwise, use the Data Directory which will be set for ClickOnce or MSI based installs
' This assumes that MSI based apps placed the data file in the standard non roaming data directory:
' C:\Documents and Settings\<UserName>\Application Data\<Company>\<Application Name>\<Version>
If AppDomain.CurrentDomain.DomainManager IsNot Nothing AndAlso _
AppDomain.CurrentDomain.DomainManager.ToString().Contains("VSHost") Then
dataDirectory = Windows.Forms.Application.StartupPath
Else
dataDirectory = Windows.Forms.Application.UserAppDataPath
End If
Me.Item("NorthwindConnectionString") = My.Settings.NorthwindConnectionString.Replace(".\", dataDirectory & "\")
End Sub
Steve Lasker