gpt4 book ai didi

c# - 读取 app.config 的 connectionStrings 元素

转载 作者:太空狗 更新时间:2023-10-29 23:03:41 24 4
gpt4 key购买 nike

我试图从我的 app.config 中读取连接字符串,但它只显示一个连接字符串,而且它也不在我的 app.config 中。

这是我的代码:

System.Diagnostics.Debugger.Break();

Configuration config =
ConfigurationManager.OpenExeConfiguration(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

foreach (var connectionString in config.ConnectionStrings.ConnectionStrings)
System.Diagnostics.Debug.Print(connectionString.ToString());

然后打印出来:

data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

当我期望它打印出自定义连接字符串到我在 app.config 文件中指定的数据库之一时。

更新

感谢大家花时间解决我的问题并尽力提供帮助。尽管你们所有人都说我可以简单地使用 ConfigurationManager.ConnectionStrings 或以这种方式访问​​任何其他小节,但我相信当我这样做时,配置是只读的。我无法对其进行任何更改,例如添加新的连接字符串或删除现有的连接字符串。我需要这样做。如果有办法在运行时修改配置文件,请告诉我。

最佳答案

它从您的 machine.config 中读取。在<connectionStrings>之后在 app.config 中插入 <clear/>.

关于c# - 读取 app.config 的 connectionStrings 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4271166/

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