[英]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.