我试图从配置文件访问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>

当前回答

右键单击项目 选择管理器NuGet包 发现系统。配置 选择Microsoft的System.Configuration.ConfigurationManager 安装

现在你可以:

using System.Configuration;

其他回答

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

您确定添加了对.NET程序集的引用,而不是其他内容吗?我会删除你的引用,然后尝试重新添加它,确保你从Visual Studio引用对话框中的. net选项卡中选择-最新版本应该是2.0.0.0。

加上这个答案,因为建议的解决方案都不适合我。

右键单击引用选项卡添加引用。 单击“程序集”选项卡 搜索“系统”。配置的 单击OK。

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

添加配置管理器文件以连接到数据库web.config