C# 配置管理器问题

发布于 2024-11-15 15:07:38 字数 1307 浏览 3 评论 0原文

我已将项目从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌生 2024-11-22 15:07:38

您必须添加对 System.configuration 的引用。

You have to add a reference the System.configuration.

山有枢 2024-11-22 15:07:38

确保您引用的是 System.Configuration 4.0。尝试删除现有引用,然后再次添加最新版本。

然后您可以获得实际的连接字符串,并创建一个 SQL 连接,如下所示:

using (SqlConnection conn = new SqlConnection(
   ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString))
{
    conn.Open();
    // do stuff
}

参考

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:

using (SqlConnection conn = new SqlConnection(
   ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString))
{
    conn.Open();
    // do stuff
}

Reference

舂唻埖巳落 2024-11-22 15:07:38

我已经解决了我的问题,将字符串“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文