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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13940697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:09:38  来源:igfitidea点击:

configurationManager does not exist in the namespace System.Configuration

c#sql-servervisual-studio-2005

提问by Mohibullah

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.ConfigurationManager.AppSettings '

'System.Configuration.ConfigurationSettings.AppSettings' 已过时。此方法已过时,已被 'System.Configuration! 取代!System.Configuration.ConfigurationManager.AppSettings '

but I have no found ConfigurationManager in that namespace and for oconnection.Open();the message is

但我在该命名空间中没有找到 ConfigurationManager 并且oconnection.Open();消息是

InvalidOperationException

无效操作异常

was unhandled.

未处理。

What can I do?

我能做什么?

回答by Khan

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 ConfigurationManagerclass enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettingsclass, which is deprecated.

来自 MSDN:ConfigurationManager该类使您能够访问机器、应用程序和用户配置信息。此类取代了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. This is caused when the connection string does not specify a data source or server. I am guessing that your connection string is empty.

参考InvalidOperationException. 这是在连接字符串未指定数据源或服务器时引起的。我猜你的连接字符串是空的。

In your web.config check the location of your connection string. If it falls under the element, then you will need to change your code to search ConnectionStringsand not AppSettings.

在您的 web.config 检查您的连接字符串的位置。如果它属于元素,那么您需要将代码更改为 searchConnectionStrings而不是AppSettings

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

回答by Amit K.S

Add a reference of : System.Configuration.dll in your project, then ConfigurationManager will be availiable

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

回答by kschieck

If you have multiple projects in the solution you have to add the reference System.Configurationto each of them for ConfigurationManagerto work in any of them.

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

回答by sirbradwick

In the latest version of Visual Studio, I had to add System.Configuration.ConfigurationManager via the NuGet package manager.

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