我目前正在创建一个登录表单,并有以下代码:

string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try
{
    using (OdbcConnection connect = new OdbcConnection(connectionString))
    {
        connect.Open();
        OdbcCommand cmd = new OdbcCommand("SELECT username, password FROM receptionist", connect);
        OdbcDataReader reader = cmd.ExecuteReader();

        if (username_login.Text == username && password_login.Text == password)
        {
            this.Hide();
            MessageBox.Show("Invalid User", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            this.Close();
        }
        else 
            MessageBox.Show("Invalid User", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        connect.Close();
    }
}
catch (OdbcException ex)
{
    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

但每当我尝试输入用户名和密码时,就会出现一个名为“配置系统初始化失败”的错误。这是什么问题,我怎么解决它?


当前回答

我重启了Visual studio甚至整个PC。 我清理了项目,重建,并删除了bin文件。

没有任何帮助,直到我把配置从x64改为x86。 它在x86上工作,但当我把它改回来时,它也工作了!

其他回答

我尝试了上面所有的解决方案,试图弄清楚为什么我的一个单元测试无法从一个完美的app.config文件中获取配置。

我有2个对相同程序集的引用,如下所示:

删除(重复)参考在黄色固定它为我。

我希望这对其他人也有用,有段时间我都快疯了。

对象中创建了副本,有时会出现错误

c:\用户\应用数据\本地\"你的应用名称"…

删除这个文件夹就可以了。试一试。

.Net核心WinForms / WPF / .Net标准类库项目的简单解决方案

步骤1:通过Nuget Manager安装System.Configuration.ConfigurationManager

步骤2:添加一个新的App.Config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Bodrum" value="Yalikavak" />    
  </appSettings>
</configuration>

step3:获取值

string value = ConfigurationManager.AppSettings.Get("Bodrum");
// value is Yalikavak

如果你从类库中调用它,那么在主项目中添加App.Config文件。

I know this has already been answered but I had exactly the same problem in my unit tests. I was tearing my hair out - adding an appSettings section, and then declaring the configuration section as per the answer. Finally found out that I had already declared an appSettings section further up my config file. Both sections pointed to my external settings file "appSettings.config" but the first appSettings element using the attribute file whilst the other used the attribute configSource. I know the question was about the connectionStrings. Sure enough, this happens if the appSettings element is the connectionStrings element being duplicated with different attributes.

希望这可以为其他人提供解决方案,以免他们重蹈我的覆辙,浪费一两个小时。感叹我们开发者的生活。有时候我们在调试上浪费的时间比在开发上还要多!

确保您的配置文件(web。Config如果web,或app.config如果windows)在你的项目开始:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" 
                      type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

            <section name="YourProjectName.Properties.Settings" 
                     type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                     requirePermission="false" />

        </sectionGroup>
    </configSections>
</configuration>

注意,在configuration元素中,第一个子元素必须是configSections元素。

在section元素的name属性中,确保将YourProjectName替换为实际项目的名称。

它发生在我身上,我在一个类库项目中创建了一个webservice,然后我复制(覆盖)配置文件(为了带来端点配置)到我的windows应用程序,我开始有同样的问题。我无意中删除了configSections。