【问题标题】:C# ConfigurationManager ProblemC# ConfigurationManager 问题
【发布时间】:2020-12-13 02:35:28
【问题描述】:

我已将项目从 framework3.5 升级到 framework4.0。现在我正在使用 Visual Studio 2010。这是我的 app.config 文件

<?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>

在这里,当我使用框架 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); 
     ....
   } 
 }
}

当我升级到框架 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

谁能告诉我如何解决这个问题。提前谢谢。

【问题讨论】:

    标签: c# .net configurationmanager


    【解决方案1】:

    您必须添加引用 System.configuration

    【讨论】:

    【解决方案2】:

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

    然后你可以得到实际的连接字符串,并创建一个这样的SQL连接:

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

    Reference

    【讨论】:

    • 这里添加 using System.Configuration 后;它不会在 System.Configuration 库中显示 ConfigurationManager。
    • 您要参考哪个版本的 System.Configuration?你可以删除它并重新添加正确的吗?
    【解决方案3】:

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

    在我的情况下工作完美

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      相关资源
      最近更新 更多