c# - 连接未关闭。连接的当前状态是 open.C#
问题描述
我正在练习我的 C# 编程技能,我相对较新,我正在尝试创建一个软件来帮助我父亲跟踪他的文档,我做得很好但遇到了这个问题。
当我运行代码时,它工作正常,当我在文本框中输入一些信息时,假设我输入 123 作为 ID,它显示“由于这已经是主键,并且 ID 已经存在,请选择其他内容”,所以我将我的 ID 重新输入为 1234 以进行测试,我收到此错误“连接未关闭。连接的当前状态为打开”我确实有 con.Open() 和 con.Close(),但我仍然无法解决问题,这是我的代码,如果有人可以提供帮助,非常感谢:
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();
}
}
}
推荐阅读
- c - 使用指针从方法中获取 char 数组值。
- android - Firebase 函数 - ReferenceError:未定义事件
- r - 为什么 slotNames 在 data.frame 上工作,即使它不是真正的 S4 对象?
- python - 使用 Python 抓取 MediaWiki 网站(特定的 html 标签)
- html - 圆的边框样式为刻度
- javascript - 类元素的点击显示不起作用
- java - Admob 横幅广告不显示
- mysql - 是否可以在 Laravel 中加入如下表格?
- python - 图像处理:鱼测量的问题
- python-3.x - 如何在某个进程结束时在 python 中启用 qt5 按钮