Changing Settings at Runtime in working project and referenced assemblies (or projects) ( including connection strings )

 
I like and need this.
 
Put this in ApplicationEvents
 

Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    SetConnectionStrings()
End Sub

 

Private Sub SetConnectionStrings()

            Dim sConString As New SqlConnectionStringBuilder
            sConString.MultipleActiveResultSets = True
            sConString.IntegratedSecurity = True

            Select Case My.Computer.Name.ToUpper
                Case “Computer1”
                    sConString.DataSource = My.Computer.Name
                Case “Computer2”
                    sConString.DataSource = My.Computer.Name ‘+ “\SQLEXPRESS”
                Case Else
                    sConString.DataSource = “<your server name>”
            End Select

            My.Settings.conStringSettings = New System.Configuration.ConnectionStringSettings
            My.Settings.conStringSettings.ProviderName = System.Data.SqlClient 

            sConString.InitialCatalog = “<your initial catalog>”
            My.Settings.<name of saved system.configuration.connectionstringsettings>.ConnectionString = sConString.ConnectionString
            My.Settings(<name of saved connection setting) = sConString.ConnectionString
            Global.<referenced assembly name>.My.MySettings.Default(“<name of saved connection setting>”) = sConString.ConnectionString
            ‘MessageBox.Show(Global.BC.My.MySettings.Default.conStrSOP)

End Sub

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: