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

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

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


当前回答

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

其他回答

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

这有点傻,但对我来说,我通过在我的代码上做一个从源代码控制获取最新信息来解决它。我认为是其他人添加了一些新的配置元素,我需要重写我的配置文件。OP显示了我得到的错误,它并没有真正地将我指向正确的方向。

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

   <?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>

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

同样的问题,我解决了我的问题,从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支持运行时元素