var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["ServerConnection"].ConnectionString = "Data Source=...";
configuration.Save();
実際の web.config を変更したくない場合は、次を使用できます。
using(OleDbConnection conn = new OleDbConnection("Type here your connection string"))
{
// Execute my code
}
または、一部のデータを保持したい場合は、接続文字列の値を別の文字列にコピーしてから変更を加えることができます..
string myNewConnectionString = ConfigurationManager.ConnectionStrings["ServerConnection"].ConnectionString;
myNewConnectionString.Replace("Data Source=Development", "Data Source=Production");