我在app.config中有以下连接字符串:
<add name="CONN" connectionString="SERVER=SERVER\SQLEXPRESS;DATABASE=TRIAL_LINK;uid=sa;pwd=trial"
providerName="System.Data.SqlClient" />
我有一个名为DBLinker的表格,在其中给用户提供了选择其他服务器和数据库的选项。 例如,我选择服务器名称为“MAILSERVER”和数据库为“Actual”。我使用以下代码覆盖app.config文件。
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim mySection As ConnectionStringsSection = DirectCast(config.GetSection("CONN"),ConnectionStringsSection)
Dim conStr As String = "SERVER=MAILSERVER;DATABASE=Actual;uid=sa;pwd=trial"
config.ConnectionStrings.ConnectionStrings("CONN").ConnectionString = conStr
config.Save(ConfigurationSaveMode.Full)
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name)
在这段代码之后,我尝试打开应用程序的登录表单。但是当我在这里尝试访问连接字符串时,它获取的是旧的字符串而不是更新后的。