【问题标题】:How to fix "the name configurationmanager does not exist in the current context"如何修复“当前上下文中不存在名称配置管理器”
【发布时间】:2012-11-05 11:41:02
【问题描述】:

我已经添加了参考 System.Configuration,但仍然收到该错误。 我正在使用VS2010。这是代码

<configuration>
<connectionStrings>
    <add name="MESConnection" connectionString="Server=.\SQLEXPRESS;Database=MES;Trusted_Connection=true"/>
</connectionStrings>
<system.web>
    <compilation debug="true" targetFramework="4.0">
        <assemblies>
            <add assembly="System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
</system.web>

以及后面的代码:

private static string _connStr = ConfigurationManager.ConnectionStrings["MESConnection"].ConnectionString;

有什么想法吗?

【问题讨论】:

  • 你有导入 System.Configuration 参考吗?
  • 是的,我添加了引用,甚至没有忘记添加 using 语句..

标签: asp.net configuration-management


【解决方案1】:

在您的 cs 代码文件顶部添加 using 语句:

using System.Configuration;

或完全限定参考:

private static string _connStr = 
    System.Configuration.ConfigurationManager.ConnectionStrings["MESConnection"].ConnectionString;

编辑

我看到您实际上没有引用正确的程序集。您需要参考 System.Configuration.dll not System.Configuration.Install.dll

在 Visual Studio 中引用 .NET 程序集的常用方法是右键单击解决方案资源管理器中的“引用”节点并选择“添加引用”...

【讨论】:

  • 我已经添加了 using 语句。并尝试添加该行。它没有响应
  • 哈哈,上帝保佑。我从不对人大喊大叫,只对电脑大喊大叫:) 谢谢 HollyStyles。看来我在错误的地方添加了参考。那些菜鸟……
【解决方案2】:
  1. 转到右侧的解决方案浏览器
  2. 右键单击引用
  3. 选择添加。

现在您可以添加配置库并使用其命名空间进行访问

【讨论】:

    猜你喜欢
    • 2014-04-18
    • 1970-01-01
    • 2021-02-07
    • 2019-06-23
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多