C# 连接没有关闭。连接';s当前状态是打开的#
我正在练习我的C#编程技能,我是一个比较新的人,我正在尝试创建一个软件来帮助我父亲跟踪他的文档,我做得很好,但遇到了这个问题 当我运行代码时,它工作正常,当我在文本框中输入一些信息时,比如说我输入123作为ID,它显示“既然这已经是主键,并且ID已经存在,请选择其他内容”,因此为了测试目的,我将ID重新输入为1234,我得到了这个错误“连接没有关闭。连接的当前状态为打开“我确实有con.open()和con.Close(),但我仍然无法解决此问题,这是我的代码,如果有人可以帮助,非常感谢:C# 连接没有关闭。连接';s当前状态是打开的#,c#,C#,我正在练习我的C#编程技能,我是一个比较新的人,我正在尝试创建一个软件来帮助我父亲跟踪他的文档,我做得很好,但遇到了这个问题 当我运行代码时,它工作正常,当我在文本框中输入一些信息时,比如说我输入123作为ID,它显示“既然这已经是主键,并且ID已经存在,请选择其他内容”,因此为了测试目的,我将ID重新输入为1234,我得到了这个错误“连接没有关闭。连接的当前状态为打开“我确实有con.open()和con.Close(),但我仍然无法解决此问题,这是我的代码,如果有人可以帮助,非常感谢: us
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Documents
{
public partial class hoadonKT : Form
{
SqlConnection con = new SqlConnection(Properties.Settings.Default.HoaDonKetThucConnection);
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
public hoadonKT()
{
InitializeComponent();
}
private void Label1_Click(object sender, EventArgs e)
{
}
private void Label1_Click_1(object sender, EventArgs e)
{
}
private void Button1_Click(object sender, EventArgs e)
{
// save button
if (textBox1.Text == "" && textBox2.Text == "")
{
MessageBox.Show("Hay dien du thong tin can thiet");
}
else
{
try
{
con.Open();
cmd = new SqlCommand(@"INSERT INTO [dbo].[KTHoaDon]
([MaDon],[TenDon])
VALUES
('"+textBox1.Text+"','"+textBox2.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Du Lieu Da Duoc Luu Tru");
fillGrid();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public void fillGrid()
{
// fill datagridview from datatable
con.Open();
da = new SqlDataAdapter("select * from KTHoaDon order by MaDon asc", con);
con.Close();
SqlCommandBuilder cd = new SqlCommandBuilder(da);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void Button1_Click_1(object sender, EventArgs e)
{
}
private void HoadonKT_Load(object sender, EventArgs e)
{
fillGrid();
}
int i;
private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
i = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[i];
textBox1.Text = row.Cells[0].Value.ToString();
textBox2.Text = row.Cells[1].Value.ToString();
}
}
}
试试这个代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Documents
{
public partial class hoadonKT : Form
{
SqlConnection con = new SqlConnection(Properties.Settings.Default.HoaDonKetThucConnection);
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
public hoadonKT()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
// save button
if (textBox1.Text == "" && textBox2.Text == "")
{
MessageBox.Show("Hay dien du thong tin can thiet");
}
else
{
try
{
string queryText = "INSERT INTO [dbo].[KTHoaDon] ([MaDon],[TenDon]) VALUES(@MaDon,@TenDon)";
using (cmd = new SqlCommand(queryText, con))
{
cmd.Parameters.AddWithValue("MaDon", textBox1.Text);
cmd.Parameters.AddWithValue("TenDon", textBox2.Text);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Du Lieu Da Duoc Luu Tru");
fillGrid();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public void fillGrid()
{
dt = new DataTable();
// fill datagridview from datatable
using (da = new SqlDataAdapter("select * from KTHoaDon order by MaDon asc", con))
{
da.Fill(dt);
}
dataGridView1.DataSource = dt;
}
private void HoadonKT_Load(object sender, EventArgs e)
{
con.Open();
fillGrid();
}
private void HoadonKT_FormClosing(object sender, FormClosingEventArgs e)
{
con.Close();
}
int i;
private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
i = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[i];
textBox1.Text = row.Cells[0].Value.ToString();
textBox2.Text = row.Cells[1].Value.ToString();
}
}
}
这就是为什么你应该总是使用这些陈述。我不太明白,你能再详细一点吗?谢谢你的回答。不要强迫用户“发明”“一个ID。创建一个自动ID列,插入时将其删除。DB将为您创建一个ID。您仍然有其他问题,但我认为这是这个问题的主要问题。Salah的意思是:您尝试插入后未关闭连接,出现异常。使用
Using
语句可以防止这种情况(跟随他的链接)。