How do I … change Config ConnectionString at runtime

See code below


Public Class RuntimeConnection

Const ConStringSettingsName = "MyEntitiesName"

Sub SetRuntimeConnection(UserNameValue As String, PasswordValue As String)
Dim currentConString = ConfigurationManager.ConnectionStrings(ConStringSettingsName).ConnectionString
Dim myWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
Dim builder As New EntityClient.EntityConnectionStringBuilder(currentConString)
Dim sqlConStrManager As New SqlClient.SqlConnectionStringBuilder(builder.ProviderConnectionString)
sqlConStrManager.UserID = UserNameValue
sqlConStrManager.Password = PasswordValue
builder.ProviderConnectionString = sqlConStrManager.ConnectionString
myWebConfig.ConnectionStrings.ConnectionStrings(ConStringSettingsName).ConnectionString = builder.ConnectionString
myWebConfig.Save()
ConfigurationManager.RefreshSection("connectionStrings")
'Dim msg = String.Concat(sqlConStrManager.ConnectionString, vbCrLf, vbCrLf, builder.ConnectionString, vbCrLf, vbCrLf, _
' myWebConfig.ConnectionStrings.ConnectionStrings(ConStringSettingsName).ConnectionString)
'MsgBox(msg)
End Sub

End Class

Also see below


Imports System.Configuration

...

Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.ConnectionStrings.ConnectionStrings("myEntities").ConnectionString = value
config.Save()
ConfigurationManager.RefreshSection("connectionStrings")
'MsgBox(config.ConnectionStrings.ConnectionStrings("myEntities").ConnectionString)

.

 

End.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: