gpt4 book ai didi

c# - 当前上下文中不存在 ConfigurationManager。我的代码有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:10 25 4
gpt4 key购买 nike

<分区>

我正在参加应用程序开发类(class),但我无法弄清楚我的上下文中出了什么问题。谁能帮帮我?我在与数据库通信方面有点陌生,所以我无法为此提出解决方案。提前致谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
String connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
try
{
if (sqlConnection.State == ConnectionState.Closed)
sqlConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}

App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<clear />
<add name="NorthwindConnectionString"
providerName="System.Data.ProviderName"
connectionString="Server=localhost;Database=Northwind;Integrated Security=true;" />
</connectionStrings>

</configuration>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com