public class WebDBHelperConfig
{
private static string filePath = "\\\\IP地址\\文件夹\\Web.config";
public static void addConnectionStrings()
{
// 打开指定的配置文件
Configuration ConfigurationInstance = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
{
ExeConfigFilename = filePath
}, ConfigurationUserLevel.None);
// 加载系统配置文件
Configuration configuration = null;
if (System.Web.HttpContext.Current != null)
{
configuration =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
}
else
{
configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
//如果要更改的连接串已经存在
if (configuration.ConnectionStrings.ConnectionStrings["DecisionData"] != null)
{
configuration.ConnectionStrings.ConnectionStrings.Remove("DecisionData");
}
// 修改配置文件
ConnectionStringSettings connectionStringSetting = new ConnectionStringSettings("DecisionData", ConfigurationInstance.ConnectionStrings.ConnectionStrings["DecisionData"].ConnectionString);
configuration.ConnectionStrings.ConnectionStrings.Add(connectionStringSetting);
// 保存更改并关闭配置文件
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
}