C# ';System.Configuration.ConfigurationErrorsException';发生在System.Configuration.dll中,System.Configure已包含在内
我在VS2013中有一个Windows窗体应用程序,我正在尝试建立数据库连接。但是,每次运行程序时,我都会收到以下错误:C# ';System.Configuration.ConfigurationErrorsException';发生在System.Configuration.dll中,System.Configure已包含在内,c#,.net,visual-studio,connection-string,app-config,C#,.net,Visual Studio,Connection String,App Config,我在VS2013中有一个Windows窗体应用程序,我正在尝试建立数据库连接。但是,每次运行程序时,我都会收到以下错误: An unhandled exception of 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll Additional information: Configuration system failed to initializ
An unhandled exception of 'System.Configuration.ConfigurationErrorsException' occurred in
System.Configuration.dll
Additional information: Configuration system failed to initialize
以下是我的文件的外观:
Program.cs
使用系统;
使用系统集合;
....
命名空间程序名
{
静态类FakeNameB
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}
表格1.cs
使用系统;
....
命名空间程序名
{
公共部分类Form1:Form
{
公共表格1()
{
字符串CS=System.Configuration.ConfigurationManager.ConnectionString[].ConnectionString.ToString();
初始化组件();
}
}
}
App.config
似乎有帮助,但我无法像建议的那样引用System.Configuration.dll。有什么想法吗 在app.config中,它应该是
而不是
:
在app.config中,它应该是
而不是
:
此特定问题的修复可以在的接受答案的注释中找到,并通过将configSections标记移动到的第一个子部分,将startup标记移动到包含的最后一个部分来解决。此特定问题的修复可以在的接受答案的注释中找到,通过将configSections标记移动为包含的第一个子节,并将startup标记移动为包含的最后一个节来解决此问题。顺便说一句,如果缺少包含,则程序根本不会编译。如果构建没有失败,则显示包含没有丢失。你认为我需要运行clean solution吗?我看你已经修复了它,但也许下次使用VS的visual designer编辑配置文件会更容易(更安全)。顺便说一句,如果你确实缺少一个包含,那么程序根本就不会编译。构建没有失败,所以看起来没有缺少包含。您认为我需要运行clean solution吗?我看您已经修复了它,但也许下次使用VS的visual designer编辑配置文件会更容易(更安全)。
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());
}
}
}
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>
<connectionStrings>
<add name="[name]" connectionString="[string]" providerName="System.Data.SqlClient" />
</connectionStrings>