C#Winform更改配置文件连接数据库不能立即生效

在程序运行过程中,配置文件只加载一次,所以动态更改后必须重新读取一次才会生效,以下是代码部分。

创新互联是一家成都网站建设、成都做网站,提供网页设计,网站设计,网站制作,建网站,按需搭建网站,网站开发公司,成立与2013年是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。

1、动态修改配置文件

          //通过Xml方式(需using System.xml;) 

           XmlDocument doc = new XmlDocument();  
            doc.Load(Application.ExecutablePath + ".config");  
            //XmlNode node = doc.SelectSingleNode(@"//add[@key='" + key + "']"); // 定位到add节点  
            XmlNode node = doc.SelectSingleNode(@"//add[@name='dbConnStr']"); // 定位到add节点
            XmlElement element = (XmlElement)node;
            element.SetAttribute("connectionString", "Data Source=.;Initial Catalog="";Integrated Security=True"); // 赋值 
            doc.Save(Application.ExecutablePath + ".config");
            ConfigurationManager.RefreshSection("connectionStrings"); // 刷新节点

2、每次操作数据库的时候重新读取配置文件节点

      string connStr =ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;


网页标题:C#Winform更改配置文件连接数据库不能立即生效
网站URL:http://csdahua.cn/article/gcgped.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流