3

大家好,我收到以下错误:“当前上下文中不存在名称‘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;
        }

    }
}
4

5 回答 5

12

您是否添加了对 System.Configuration.dll 的引用?

于 2012-04-09T11:03:37.087 回答
5

您必须添加对 System.Configuration.dll 的引用

当您右键单击并单击“添加引用”按钮

在打开的弹出窗口中单击 .Net 选项卡

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

于 2012-04-09T11:10:28.060 回答
3

这是路径:

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

(希望能帮助到你 :))

于 2016-07-14T10:05:00.597 回答
1

您使用的是哪个版本的 .NET Framework 和 Visual Studio?

ConfigurationManager 仅在 .NET 2 及更高版本中可用。如果您使用的是 .NET Framework 1.x 和 Visual Studio 2002/2003,则根本无法使用此类。

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

单击此页面顶部的“其他版本”链接,您将看到它支持的所有版本。

于 2012-04-09T11:40:42.027 回答
1

我在使用 .Net Core 时遇到了同样的问题,但使用Install-Package System.Configuration.ConfigurationManager在包管理器中运行的 .NET 为我解决了这个问题。

于 2021-06-22T11:54:01.747 回答