在网上找了好多都大同小异,appsettings和ConnectionStrings两种,两种都试了还是没反应,也没报错,可以查询到但是删除和添加修改都不起作用,不知道是怎么回事,跪求大神解答
这是ConnectionStrings的
///<summary>
///更新连接字符串
///</summary>
///<param name="newName">连接字符串名称</param>
///<param name="newConString">连接字符串内容</param>
///<param name="newProviderName">数据提供程序名称</param>
public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName)
{
//指定config文件读取
string file = System.Windows.Forms.Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(file);
bool exist = false; //记录该连接串是否已经存在
//如果要更改的连接串已经存在
if (config.ConnectionStrings.ConnectionStrings[newName] != null)
{
exist = true;
}
// 如果连接串已存在,首先删除它
if (exist)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
//新建一个连接字符串实例
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName, newConString, newProviderName);
// 将新的连接串添加到配置文件中.
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
// 保存对配置文件所作的更改
config.Save(ConfigurationSaveMode.Modified);
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("ConnectionStrings");
}
这是appsettings的
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationElement keyValue = new KeyValueConfigurationElement(connectName, connectString);
AppSettingsSection appsetings = config.AppSettings;
appsetings.Settings.Add(keyValue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(connectName);
é£æ¯ä¸æ¯å°±è¦ç¨XMLå¤çèç¹ï¼
追çé¢ãããæ¢ç¶æ¯éæåäºï¼ä½ ç¨xmlå¤çä¹è¡ï¼ç´æ¥è¯»åææææ¬ç¶åå¨éé¢æ¹äºåååå»ä¹è¡
è¿é½éä½ åäº