Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection cnnSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
cnnSection.ConnectionStrings.Clear();
cnnSection.ConnectionStrings.Add(new System.Configuration.ConnectionStringSettings("SQL SERVER", value, "System.Data.SqlClient"));
config.Save();
第一个函数的参数是webconfig的位置,如果为null是framwork的根目录下的webconfig,当然默认是没有权限去更改的,这里的“~”意思是当前程序的根目录。当然如果为了保险还可以加入一些权限控制的东西。
读写WebConfig
最新推荐文章于 2019-06-17 11:00:17 发布
本文介绍了一种在.NET应用程序中更新Web.config文件里的数据库连接字符串的方法。通过使用ConfigurationManager类和ConnectionStringsSection对象,可以实现对连接字符串的安全修改。
170

被折叠的 条评论
为什么被折叠?



