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

标签 c# sql-server visual-studio-2005

我使用了以下命名空间将我的项目连接到 sql server:

using System.Configuration;

也用过

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

当我运行程序时,发生错误并显示消息

'System.Configuration.ConfigurationSettings.AppSettings' is obsolete.This method is obsolete,it has been replaced by 'System.Configuration! System.Configuration.ConfigurationManager.AppSettings '

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

InvalidOperationException

未处理。

我能做什么?

最佳答案

转到引用,并添加对 System.Configuration 的引用

完成此操作后,您应该能够引用 System.Configuration.ConfigurationManager

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

来自 MSDN: ConfigurationManager 类使您能够访问机器、应用程序和用户配置信息。此类替换了已弃用的 ConfigurationSettings 类。

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


编辑:附加信息

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

在您的 web.config 中检查您的连接字符串的位置。如果它属于该元素,则您需要更改代码以搜索 ConnectionStrings 而不是 AppSettings

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

关于c# - 命名空间 System.Configuration 中不存在 configurationManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13940697/

相关文章:

asp.net - Visual Studio 团队套件: How to web test programmatically?

c# - MySQL 本地/在线服务器不会响应 C#/.NET

c# - 对于复杂的搜索查询,Google 搜索返回 503 错误

c# - 在制作动态 URL 时执行字符串连接的最佳方法是什么?

c# - 使用 SelfHosting 时,我可以在 app.config 中自动托管所有服务吗?

sql - 从 SQL Server 中的 VARCHAR 中删除非数字字符的最快方法

visual-studio-2005 - cmake:如何制作一个脚本来复制我的程序随附的数据文件

sql - 与其他SQL子句相比,PARTITION BY子句的执行顺序是什么?

sql - Microsoft SQL 浏览器客户端

c++ - 如何在项目中实现预编译头文件