При запуске программы вылезает исключение System.Configuration.ConfigurationErrorsException: "Система конфигурации не прошла инициализацию",
При запуске программы вылезает исключение System.Configuration.ConfigurationErrorsException: "Система конфигурации не прошла инициализацию", подскажите как это исправить. Код:
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lab_9
{
public partial class Form1 : Form
{
private string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public Form1()
{
InitializeComponent();
}
private void CreateDatabase(string sqlQuery)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
SqlCommand command = new SqlCommand("create database " + sqlQuery, connection);
command.ExecuteNonQuery();
outStat.Text = " база данных успешно создана!";
}
catch (Exception e)
{
outStat.Text = " ошибка создания базы данных!";
}
}
}
private void AddTable(string tableName)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("create table " + tableName + " ( " + tableName + "_ID Integer NOT NULL PRIMARY KEY IDENTITY(1,1))", connection);
command.ExecuteNonQuery();
outStat.Text = " таблица успешно добавлена!";
}
}
catch (Exception e)
{
outStat.Text = " ошибка добавления таблицы!";
}
}
private void AddColumn(string tableName, string columnName, string type)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
SqlCommand command = new SqlCommand("alter table " + tableName + " add " + columnName + " " + type, connection);
command.ExecuteNonQuery();
outStat.Text = "Статус: столбец успешно добавлен!";
}
catch (Exception e)
{
outStat.Text = "Статус: ошибка добавления столбца!";
}
}
}
private void DeleteColumn(string tableName, string columnName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
SqlCommand command = new SqlCommand("alter table " + tableName + " drop column " + columnName,
connection);
command.ExecuteNonQuery();
outStat.Text = "Статус: столбец успешно удален!";
}
catch (Exception e)
{
outStat.Text = "Статус: ошибка удаления столбца, возможно имя задано не верно!";
}
}
}
private void butNewDB_Click(object sender, EventArgs e)
{
CreateDatabase(NameDB.Text);
}
private void butNewTable_Click(object sender, EventArgs e)
{
AddTable(nameTable.Text);
}
private void butNewRow_Click(object sender, EventArgs e)
{
AddColumn(nameTable.Text, nameRow.Text, inpVar.Text);
}
private void butDelRow_Click(object sender, EventArgs e)
{
DeleteColumn(nameTable.Text, nameRow.Text);
}
}
}
Исключение появляется во время выполнения этой строки:
private string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
Appi.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<connectionString>
<add name = "DefaultConnection"
connectionString ="VladPC\SQLEXPRESS;
Initial Catalog = testDB; Integrated Security = True"
providerName ="System.Data.SQLClient"/>
</connectionString>
</configuration>