C# 配置管理器问题
我已将项目从framework3.5升级到framework4.0。现在我正在使用Visual studio 2010。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DBConnectionString" value="User ID=sa;Password=password123;Initial Catalog=DishTV_Voting;Persist Security Info=True;Data Source=ENMEDIA-50CB48D"/>
</appSettings>
</configuration>
这是我的app.config文件当我使用framework 3.5时,我使用的配置文件为
using System.Configuration;
namespace Voting_Editor_Tool_New
{
public partial class Voting_Editor_Tool : Form
{
SqlConnection myConnection;
string connectString = ConfigurationSettings.AppSettings["DBConnectionString"];
public void getdata()
{
myConnection = new SqlConnection(connectString);
....
}
}
}
当我升级到framework 4.0时该行
ConfigurationSettings.AppSettings["DBConnectionString"];
显示一条警告消息,因为
“System.Configuration.ConfigurationSettings.AppSettings”已过时:“此方法已过时,已被替换为System.Configuration!System.Configuration.ConfigurationManager.AppSettings'。
我尝试使用 ConfigurationManager.AppSettings["DBConnectionString"];但它显示一个错误 任何
The name 'ConfigurationManager' does not exist in the current context
人都可以解决这个问题。提前致谢。
I have upgrade the project from framework3.5 to framework4.0.Right Now i am using Visual studio 2010.Here is my app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DBConnectionString" value="User ID=sa;Password=password123;Initial Catalog=DishTV_Voting;Persist Security Info=True;Data Source=ENMEDIA-50CB48D"/>
</appSettings>
</configuration>
Here when i am working with framework 3.5 I used the config file as
using System.Configuration;
namespace Voting_Editor_Tool_New
{
public partial class Voting_Editor_Tool : Form
{
SqlConnection myConnection;
string connectString = ConfigurationSettings.AppSettings["DBConnectionString"];
public void getdata()
{
myConnection = new SqlConnection(connectString);
....
}
}
}
When I upgrade to framework 4.0 the line
ConfigurationSettings.AppSettings["DBConnectionString"];
shows a warning message as
'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'.
I tried with ConfigurationManager.AppSettings["DBConnectionString"]; but it shows a error
as
The name 'ConfigurationManager' does not exist in the current context
Can anyone just how to solve the problem.Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须添加对 System.configuration 的引用。
You have to add a reference the System.configuration.
确保您引用的是
System.Configuration
4.0。尝试删除现有引用,然后再次添加最新版本。然后您可以获得实际的连接字符串,并创建一个 SQL 连接,如下所示:
参考
Make sure you are referencing
System.Configuration
4.0. Try to remove the existing reference, and add the latest version of it again.Then you can get the actual connection string, and create an SQL connection as such:
Reference
我已经解决了我的问题,将字符串“connectString”更改为真正的字符串,即数据库的地址。
示例:this = myConnection = new SqlConnection(connectString);
对于这个 = myConnection = new SqlConnection("User ID=sa;Password=password123;Initial Catalog=DishTV_Voting;Persist Security Info=True;Data Source=ENMEDIA-50CB48D")
在我的情况下工作完美
I had solved my problem changing the string "connectString" for the really string that is the address of your DB.
Exemple: this = myConnection = new SqlConnection(connectString);
For this = myConnection = new SqlConnection("User ID=sa;Password=password123;Initial Catalog=DishTV_Voting;Persist Security Info=True;Data Source=ENMEDIA-50CB48D")
In my case works perfect