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

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

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


当前回答

我也面临着同样的问题,但我不小心写了 如果不写the,前一个应该放在这个标记中。因此出现了“配置系统初始化失败”错误。 希望对大家有所帮助

其他回答

在我的例子中,我在app.config文件中有两个configsections。删除隐藏在代码行后,应用程序工作正常。

所以如果有人有同样的问题,先检查你是否有重复的配置部分。

哇,我花了好长时间才弄明白这个。由于某种原因,在AssemblyInfo.cs中更改属性[assembly: AssemblyCompany("CompanyName")]使此错误消失。我引用了一个具有不同值的属性[assembly: AssemblyCompany("CompanyName")]的项目。我确保两个项目具有相同的属性值,它工作得很好!

删除c:\Users\username\AppData\Local\appname和c:\ users \username\ appdata \ roam \appname目录下的旧配置文件,然后尝试重新启动应用程序。

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.

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

如果你有一个自定义的section,你需要在configSections下面的configurations标签下面提到它。

请检查您的转换文件,确保您删除了不必要的标签。只有将要变化的部分需要在转换文件中。如果不需要,不要在转换文件中提到配置部分。这也会引起问题。

如果您在机器中有任何语法错误。配置时,也会出现此错误。