【问题标题】:Not able to retrieve ConnectionString from app.config in .NET无法从 .NET 中的 app.config 检索 ConnectionString
【发布时间】:2020-02-23 02:22:48
【问题描述】:

这是一个 Windows 应用程序。它与本地 SQL 数据库对话,连接字符串保存在 app.config 中,即:

代码:

System.Configuration.ConfigurationManager.AppSettings.Get("CaseMgmtConn")

什么都没有

我有以下设置:

这里有什么问题?非常感谢。

更新:

我相信这里一定缺少一些东西,因为我无法得到任何东西:

System.Configuration.ConfigurationManager.ConnectionStrings("CaseMgmtConn")

什么都没有

这会返回 Nothing:

【问题讨论】:

    标签: .net connection-string


    【解决方案1】:

    是的,我们可以在 ConfigurationManager 的帮助下从配置文件中的 ConnectionStrings 字段访问字段。但不是代码: System.Configuration.ConfigurationManager.AppSettings.Get("CaseMgmtConn");

    推荐:- 字符串 ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["FullValueGivenInName"].ConnectionString;

    【讨论】:

      【解决方案2】:

      从 app.config 获取连接字符串的唯一方法是访问/读取 ConfigurationManager.ConnectionStrings(搅拌数组)属性,然后使用数组中的索引器访问连接字符串。 ConfugurationManager.AppSettings 不是获取连接字符串的属性。

      例子:

      var constring = ConnectionManager.ConnectionStrings["UseCaseMgmt.My.Settings.CaseMgmtConn"].ConnectionString;
      

      欲了解更多信息,请访问官方文档:https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.connectionstrings?view=netframework-4.7

      【讨论】:

        猜你喜欢
        • 2016-11-02
        • 2019-01-23
        • 2011-01-11
        • 2013-03-19
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        • 2020-10-12
        • 2023-04-03
        相关资源
        最近更新 更多