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

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);
}

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


当前回答

经过长时间的搜索,我意识到,这个异常有一个内部异常,它确切地告诉你配置文件出了什么问题

其他回答

.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文件。

如果你正在处理Azure WebJob -我必须在升级到最新的4.6.1后删除以下内容。

  <compilation debug="true" targetFramework="4.6.1">
    <assemblies>
      <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </assemblies>
  </compilation>

希望这能有所帮助。

如果您在其中有一些“特殊”字符,请尝试将.config文件保存为utf-8。这就是我的控制台应用程序的问题所在。

我在卸载Oracle客户端驱动程序后开始遇到这个问题,它删除了我的C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\machine.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.

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