gpt4 book ai didi

c# - VS2005 C# 以编程方式更改 app.config 中包含的连接字符串

转载 作者:太空狗 更新时间:2023-10-29 17:33:07 25 4
gpt4 key购买 nike

想以编程方式更改数据库的连接字符串,该数据库在 Windows 应用程序中使用 asp.net 的成员资格提供程序。 system.configuration 命名空间允许更改用户设置,但是,我们想调整一个应用程序设置?是否需要编写一个利用 XML 修改类的类?是否需要删除当前连接(可以选择一个连接来清除)并添加一个新连接?可以调整现有的连接字符串吗?

最佳答案

必须做这件事。这是对我有用的代码:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["Blah"].ConnectionString = "Data Source=blah;Initial Catalog=blah;UID=blah;password=blah";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

关于c# - VS2005 C# 以编程方式更改 app.config 中包含的连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com