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)
このコードの後、アプリケーションのログインフォームを開こうとしています。しかし、ここで接続文字列にアクセスしようとすると、更新された文字列ではなく、以前の文字列をフェッチしています。