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

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

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


当前回答

同样的问题,我解决了我的问题,从App.config删除version ="v3.5"。

之前

 <?xml version="1.0" encoding="utf-8"?>
  <configuration>

  <startup>

 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
 </startup>
 <supportedRuntime version="v3.5" />//Remove this
</configuration>

解决方案

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
  </configuration>

下面是如何使用版本

MSDN支持运行时元素

其他回答

如果你已经在App.Config中添加了自己的自定义配置部分,请确保你已经在<configSections>元素中定义了该部分。我添加了我的配置XML,但忘记在上面声明配置部分——这导致异常“配置系统初始化失败”。

如果你有用户范围设置,你也可以有一个用户。配置文件在[Userfolder]\AppData\Local\[ProjectName]文件夹中的某处。

如果您稍后删除用户范围设置的用户。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.

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

我通过使用下面的代码解决了这个问题

   <?xml version="1.0" encoding="utf-8" ?>
<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>
  <appSettings>
    <add key="SPUserName" value="TestUser" />
    <add key="SPPassword" value="UserPWD" />
  </appSettings>
</configuration>

我在MSTest类中遇到了同样的问题:Marlon Grech在他的文章中说:“必须将元素定义为App.config中的第一个元素。”

确保这是元素下面的第一个元素。我把AppSettings放在第一位。