“The name 'ConfigurationManager' does not exist in the current context”未在当前上下文中存在。

5 浏览
0 Comments

“The name 'ConfigurationManager' does not exist in the current context”未在当前上下文中存在。

我正在尝试从配置文件中访问connectionStrings。代码是ASP.NET + C#。我已经添加了System.Configuration进行参考,并且还在using中提到。但仍然不接受程序集。

我正在使用VSTS 2008。有什么想法可能是原因?

另一个奇怪的事情是程序集名称显示为“System.configuration”,其中包含小写的“c”,这不是其他System程序集如此显示的方式。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace Utility
{
    public class CommonVariables
    {
        public static String ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["EmployeeEntities"].ConnectionString; }
        }  
    }  
}

配置:



  
    
  

admin 更改状态以发布 2023年5月20日
0
0 Comments

在您的项目中,右键单击“添加引用...”,在.NET选项卡中找到System.Configuration组件名称并单击确定。

使用System.Configuration告诉编译器/IntelliSense搜索该命名空间中使用的任何类。否则,每次都必须使用完整名称(System.Configuration.ConfigurationManager)。但是,如果您不添加引用,该命名空间/类将无法找到任何地方。

请注意,DLL可以有任何名称空间,因此文件System.Configuration.dll在理论上可以具有名称空间Some.Random.Name。为了清晰/一致通常是相同的,但有例外。

0
0 Comments

不仅需要使用命名空间 System.Configuration,您还需要通过以下步骤添加对程序集 System.Configuration.dll 的引用:

  1. 右键单击“引用/依赖项
  2. 选择“添加引用”
  3. 找到并添加System.Configuration

这样肯定可以工作。
对于NameValueCollection,您需要编写:

using System.Collections.Specialized;

0