利用App.Config动态读写数据库连接字符串
时间:2010-09-01 来源:悟通
主要实现代码:
代码 using System.Configuration;using System.Data.OleDb;
//读
string strconn = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
//写
private void saveConnection(string connstring)
{
//保存连接
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Remove("ConnString");
ConnectionStringSettings mySettings = new ConnectionStringSettings("ConnString", connstring, "SQLOLEDB");
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
config.Save(ConfigurationSaveMode.Modified);
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("ConnectionStrings");
}
点此下载
相关阅读 更多 +