我试图从配置文件中访问 connectionStrings
. 代码是ASP.NET C# . 我已将 System.Configuration
添加到参考中,并且还提到了使用 . 但它仍然不接受集会 .
我正在使用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="Data Source=localhost;Initial Catalog=qbank;Persist Security Info=True;User ID=**;Password=****;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
13 回答
我已经为这个问题找到了更好的解决方案
configurationmanager does not exist in the current context
.要从
web.config
读取连接字符串,我们需要使用ConfigurationManager
类及其方法 . 如果要使用,则需要使用System.Configuration
添加命名空间;虽然您使用了此命名空间,但在尝试使用
ConfigurationManager
类时,系统会显示错误“当前上下文中不存在configurationmanager” . 要解决这个问题:它不仅需要使用 namespace
System.Configuration
. 您还必须添加对 assemblySystem.Configuration.dll
的引用右键单击引用/依赖项
选择添加参考
查找并添加
System.Configuration
.这肯定会起作用 . 同样对于
NameValueCollection
你必须写:在项目中,右键单击“添加引用...”,在“.NET”选项卡中,找到
System.Configuration
组件名称,然后单击“确定” .using System.Configuration
告诉编译器/ IntelliSense在该命名空间中搜索您使用的任何类 . 否则,您每次都必须使用全名(System.Configuration.ConfigurationManager
) . 但是,如果您不添加引用,则无法在任何位置找到该命名空间/类 .请注意,DLL可以具有任何名称空间,因此理论上文件
System.Configuration.dll
可以具有名称空间Some.Random.Name
. 为了清晰/一致,它们通常是相同的,但也有例外 .好的......重新启动VSTS后就可以了 . link为同样的问题提出了解决方案 . 希望我以前见过它 . :)
添加此答案,因为没有一个建议的解决方案适合我 .
如果您收到很多警告(在我的情况下是64个解决方案!)就像
因为您正在升级旧项目,所以可以节省大量时间,如下所示:
添加
System.Configuration
作为 References 部分的参考 .将以下两个
using
语句添加到每个类(.cs
)文件的顶部:using System.Configuration; using ConfigurationSettings = System.Configuration.ConfigurationManager;
通过这种改变所有的出现
现在将引用正确的配置管理器,不再是已弃用的配置管理器,并且所有CS0618警告将立即消失 .
当然,请记住,这是一个快速的黑客 . 从长远来看,您应该考虑重构代码 .
您确定已添加对.NET程序集的引用而不是其他内容吗?我将删除您的引用,然后尝试重新添加它,确保您从Visual Studio参考对话框中的.NET选项卡中选择 - 最新版本应该是GAC中的2.0.0.0 .
要进行健全性检查,请尝试创建新的Web应用程序项目,打开Default.aspx页面的代码 . 在Page_Load中添加一行以访问您的连接字符串 .
它应该默认添加System.Configuration作为引用 . 您还应该在代码文件的顶部看到using语句 .
我的代码隐藏文件现在看起来像这样编译没有问题 .
假设我的web.config中有一个连接字符串,其名称等于“MyConnectionStringName”,如此...
是的,我知道这是基本的 . 但是,如果你没有任何更好的想法,有时候检查你知道应该工作的非常简单的事情是有帮助的 .
如果此代码位于单独的项目中,如库项目 . 不要伪造添加对system.configuration的引用 .
右键单击Project
选择管理器NuGet包
查找System.Configuration
选择Microsoft的System.Configuration.ConfigurationManager
安装
现在你可以:
如果您错误地添加对不同的不相关项目的引用,也可能会出现此错误 . 检查是否适用您 .
要解决此问题,请转到解决方案资源管理器并右键单击引用,然后单击添加引用并选择.net并查找system.configuration选择单击确定
将
System.Configuration
添加为所有项目的参考将解决此问题 .转到
Project
- >Add Reference
在出现的框中,单击左侧列表中的
All assemblies
列表选项卡 .在中央列表中,滚动到
System.Configuration
并确保选中该框 .单击确定以应用,您现在可以访问
ConfigurationManager
类 .