我试图从配置文件访问connectionStrings。代码是ASP。Net + c#。我已经添加了System。配置参考,也提到与使用。但它仍然不接受集会。

我正在使用VSTS 2008。知道是什么原因吗?

另一个奇怪的地方是程序集名称显示为“System”。configuration",小写c,这不是其他系统程序集的名称显示方式。

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; }
        }  
    }  
}

配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="qbankEntities" connectionString="metadata=res://*/qbankModel.csdl|res://*/qbankModel.ssdl|res://*/qbankModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=qbank;Persist Security Info=True;User ID=**;Password=****;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

当前回答

好吧. .重启VSTS后正常工作。这个链接给出了同样问题的解决方案。真希望我能早点看到。:)

其他回答

针对Visual Studio 2019

右键单击项目,去管理NuGet包。

搜索System。配置并安装System.Configuration.ConfigurationManager

现在在您的代码中,配置管理器将被识别。

添加系统。配置作为所有项目的参考将解决这个问题。

进入项目->添加引用 在出现的框中,单击左侧列表中的“所有程序集”列表选项卡。 在中央列表中,滚动到System。配置并确保复选框被选中。 单击ok以应用,现在就可以访问ConfigurationManager类了。

不仅需要使用名称空间System.Configuration。您还必须将引用添加到程序集System.Configuration.dll,通过

右键单击引用/依赖项 选择添加参考 找到并添加System.Configuration。

这肯定有用。 同样,对于NameValueCollection,你必须写:

using System.Collections.Specialized;

好吧. .重启VSTS后正常工作。这个链接给出了同样问题的解决方案。真希望我能早点看到。:)

要解决这个问题,请打开解决方案资源管理器,右键单击引用,然后单击添加引用,选择。net,然后查找系统。配置选择单击确定