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

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

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


当前回答

在我的例子中,在我的.edmx文件中,我运行了“从数据库更新模型”命令。这个命令给我的app.config文件添加了一个不必要的连接字符串。我删除了连接字符串,一切又好了。

其他回答

值得注意的是,如果你在app.config中添加连接字符串之类的东西,如果你在已定义的配置部分之外添加项目,它不会立即报错,但当你尝试访问它时,你可能会得到上述错误。

折叠所有主要部分,并确保在已定义的部分之外没有项目。很明显,当你真正发现它的时候。

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

如果您稍后删除用户范围设置的用户。Config不会自动被删除,它的存在可能会导致相同的错误消息。删除文件夹对我有用。

我只有这个,这是因为我有一个<configuration>元素嵌套在<configuration>元素中。

确保您的配置文件(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。

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

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