2012-04-09 72 views
2

嗨存在每一个我收到以下错误:“‘ConfigurationManager中’这个名字并不在当前的背景下存在”“ConfigurationManager中”这个名字并不在当前的背景下

// namespaces 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.IO; 

namespace Database1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     public static string GetConnectionString(string strConnection) 
     { 
      //variable to hold our connection string for returning it 

      string strReturn = ""; 
      //check to see if the user provided a connection string name 
      //this is for if your application has more than one connection string 

      if (!string.IsNullOrEmpty(strConnection)) //a connection string name was   
      { 
       //get the connection string by the name provided 
       strReturn = ConfigurationManager.ConnectionStrings[strConnection].ConnectionString; 

      } 
      else //no connection string name was provided 
      { 
       //get the default connection string 
       strReturn = ConfigurationManager.ConnectionStrings["YourConnectionName"].ConnectionString; 
      } 
      //return the connection string to the calling method 
      return strReturn; 
     } 

    } 
} 

回答

11

你有没有加入参考System.Configuration.dll?

+0

不,我没有添加此引用和我我无法在添加引用标签中找到该引用如何找到它? – user1312412 2012-04-09 11:09:26

+0

不确定,但不应该消息是类型或命名空间'ConfigurationManager'这种情况下找不到? – V4Vendetta 2012-04-09 11:15:30

+0

你使用的是什么版本的Visual Studio和.NET Framework? – Joshua 2012-04-09 11:37:49

3

您必须添加参考System.Configuration.dll

当你点击右键,在弹出开放的Add Reference按钮点击

单击.NET选项卡

在那里,你应该能够找到System.Configuration.dll文件

+0

没有它没有我有尝试过,但我无法找到System.Configuration.dll文件在这个标签 – user1312412 2012-04-09 11:15:27

+0

奇怪,你不能在那里找到它?也许你需要重启VS – V4Vendetta 2012-04-09 11:27:41

+0

或者你可能需要重新安装.Net框架? – Pete 2012-04-09 11:28:53

1

这是路径:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Configuration.dll 

,你也可以到:System:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Configuration.Install.dll 

添加引用(希望它能帮助:))

相关问题