简体   繁体   English

命名空间 System.Configuration 中不存在 configurationManager

[英]configurationManager does not exist in the namespace System.Configuration

I have used the following namespace to connect my project to the sql server:我使用以下命名空间将我的项目连接到 sql server:

using System.Configuration;

and also used并且还使用了

string str=System.Configuration.ConfigurationSettings.AppSettings["myconnection"];
SqlConnection oconnection = new SqlConnection(str);
oconnection.Open();

when I run the program ,an error has been occurred and show the message当我运行程序时,发生错误并显示消息

'System.Configuration.ConfigurationSettings.AppSettings' is obsolete.This method is obsolete,it has been replaced by 'System.Configuration! 'System.Configuration.ConfigurationSettings.AppSettings' 已过时。此方法已过时,已被 'System.Configuration! 取代! System.Configuration.ConfigurationManager.AppSettings ' System.Configuration.ConfigurationManager.AppSettings '

but I have no found ConfigurationManager in that namespace and for oconnection.Open();但我在该命名空间和oconnection.Open();没有找到 ConfigurationManager oconnection.Open(); the message is消息是

InvalidOperationException无效操作异常

was unhandled.未处理。

What can I do?我能做什么?

Go to references, and add a reference to System.Configuration转到引用,并添加对System.Configuration的引用

Once you have done this, you should be able to reference System.Configuration.ConfigurationManager .完成此操作后,您应该能够引用System.Configuration.ConfigurationManager

string str = System.Configuration.ConfigurationManager.AppSettings["myconnection"];
SqlConnection oconnection = new SqlConnection(str);
oconnection.Open();

From MSDN: The ConfigurationManager class enables you to access machine, application, and user configuration information.来自 MSDN: ConfigurationManager类使您能够访问机器、应用程序和用户配置信息。 This class replaces the ConfigurationSettings class, which is deprecated.此类替换已弃用的ConfigurationSettings类。

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx


Edit: Addiitonal Information编辑:附加信息

In reference to the InvalidOperationException .参考InvalidOperationException This is caused when the connection string does not specify a data source or server.这是在连接字符串未指定数据源或服务器时引起的。 I am guessing that your connection string is empty.我猜你的连接字符串是空的。

In your web.config check the location of your connection string.在您的 web.config 检查您的连接字符串的位置。 If it falls under the element, then you will need to change your code to search ConnectionStrings and not AppSettings .如果它属于该元素,那么您将需要更改代码以搜索ConnectionStrings而不是AppSettings

string str = System.Configuration.ConfigurationManager.
    ConnectionStrings["myconnection"].ConnectionString;

如果解决方案中有多个项目,则必须将参考System.Configuration添加到每个项目中, ConfigurationManager才能在其中任何一个项目中工作。

在您的项目中添加一个引用:System.Configuration.dll,然后ConfigurationManager 将可用

在最新版本的 Visual Studio 中,我不得不通过 NuGet 包管理器添加 System.Configuration.ConfigurationManager。

Install System.Configuration from nuget package.从 nuget 包安装 System.Configuration。 Then add reference.然后添加引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 .net 标准中的命名空间“System.Configuration”错误中不存在类型或命名空间名称“ConfigurationManager”? - How to solve type or namespace name 'ConfigurationManager' does not exist in the namespace 'System.Configuration' error in .net standard? 命名空间“System.Configuration”中不存在类型或命名空间名称“UserScopedSettingAttributeAttribute” - The type or namespace name 'UserScopedSettingAttributeAttribute' does not exist in the namespace 'System.Configuration' ConfigurationManager 存在于 System.Configuration.ConfigurationManager 和 System.Configuration 中 - ConfigurationManager exists in both System.Configuration.ConfigurationManager and in System.Configuration 尽管使用System.Configuration仍无法识别ConfigurationManager - ConfigurationManager unrecognized despite using System.Configuration 公用库和System.Configuration命名空间 - Common Library and the System.Configuration namespace ConfigurationManager.ConnectionStrings[...] 即使在包含“使用 System.Configuration”之后也无法使用 - ConfigurationManager.ConnectionStrings[...] can't be used even after including "using System.Configuration" 系统.配置配置管理器 - System.Configuration Configuration Manager 名称空间中不存在Configuration Manager - Configuration Manager does not exist in namespace 定制安装程序类中的System.Configuration - System.Configuration in a custom installer class 与XMLSerializer相比,System.Configuration有什么好处? - What are the benefits of System.Configuration over XMLSerializer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM