【问题标题】:Getting the connectionstring in .net 4在.net 4中获取连接字符串
【发布时间】:2021-04-27 01:54:32
【问题描述】:

我想从app.config 文件中获取一个连接字符串。

connectionString = System.Configuration.ConfigurationSettings.AppSettings["DBEntities"];

但它不起作用。是空的。

我无法访问 System.Configuration.ConfigurationManager,因为它是 .NET 4。 如何从app.config 获取我的连接字符串?

【问题讨论】:

  • 你能用连接字符串部分发布你的 web.config
  • ConfigurationManager 不是 .net 4 中的选项

标签: c# .net .net-4.0 entity-framework-4


【解决方案1】:

使用

string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString; 

确保在您的项目中添加对System.configuration 的引用。

【讨论】:

    【解决方案2】:

    在 .net 4 中你必须使用:

    ConfigurationManager.ConnectionStrings["name of connection string in web.config"]
    

    更多关于它的信息是herehere

    【讨论】:

    • +1 但问题被标记为c# 所以你想要() 而不是[]
    【解决方案3】:

    向您的项目添加对 System.Configuration 的引用并使用 ConnectionStrings 而不是 AppSettings

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-06
      • 2012-09-24
      • 2019-04-21
      • 2012-06-04
      • 2010-10-02
      • 1970-01-01
      • 2021-03-10
      相关资源
      最近更新 更多