【问题标题】:'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll, System.Configure already includedSystem.Configuration.dll 中发生“System.Configuration.ConfigurationErrorsException”,已包含 System.Configure
【发布时间】:2016-11-29 09:54:24
【问题描述】:

我在 VS 2013 中有一个 Windows 窗体应用程序,我正在尝试建立数据库连接。但是,每次我运行程序时,都会收到以下错误:

An unhandled exception of 'System.Configuration.ConfigurationErrorsException' occurred in         
System.Configuration.dll

Additional information: Configuration system failed to initialize

这是我的文件的样子:

程序.cs

using System;
using System.Collections;
....

namespace ProgramName
{
    static class FakeNameB
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.cs

using System;
....

namespace ProgramName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            string CS = System.Configuration.ConfigurationManager.ConnectionStrings[<nameInApp.config>].ConnectionString.ToString();

            InitializeComponent();
        }
    }
}

应用程序配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ProgramName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections> 
    <configurationStrings>
        <add name = <name> connectionString = "Data Source=<dataSource>;Initial Catalog=<catalog>;Integrated Security=True;" providerName="System.Data.SqlClient"></add> 
    </configurationStrings>
</configuration>

this post 中的任何建议似乎都没有帮助,而且我无法像this post 建议的那样引用 System.Configuration.dll。有任何想法吗?

【问题讨论】:

  • 只是一个“顺便说一句” - 如果您确实缺少包含,则程序根本不会编译
  • 构建没有失败,所以看起来包含没有丢失。你认为我需要运行干净的解决方案吗?
  • 我看到你已经修复了它,但也许下次使用 VS 的可视化设计器编辑配置文件会更容易(也更安全)。

标签: c# .net visual-studio connection-string app-config


【解决方案1】:

可以在this post 的已接受答案的 cmets 中找到此特定问题的修复程序,并通过将 configSections 标记移动到第一个子项并将启动标记移动到包含的最后一个部分来解决。

【讨论】:

    【解决方案2】:

    在您的 app.config 中,它应该是 &lt;connectionStrings&gt; 而不是 &lt;configurationStrings&gt;

    <connectionStrings>
        <add name="[name]" connectionString="[string]" providerName="System.Data.SqlClient" /> 
    </connectionStrings>
    

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 2016-09-05
      相关资源
      最近更新 更多