//删除一行
private void delete_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(DRIVER);
conn.Open();
SqlDataAdapter daAuthors = new SqlDataAdapter(sql_select, conn);
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs, SchemaType.Source, "m_task_001"); //FillSchema加载表的架构和数据,有了架构,表就知道哪个列是它的主键,同时 Rows 集合的 Find 方法也就可用了。
daAuthors.Fill(dsPubs, "m_task_001");
DataTable tblAuthors;
tblAuthors = dsPubs.Tables["m_task_001"];
if (MessageBox.Show("确实要删除该行吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
DataRow drCurrent;
drCurrent = tblAuthors.Rows.Find(GSR_Fid());
drCurrent.Delete();
//SqlCommandBuilder 提供自动生成单表命令的一种方式,这些命令用于协调使用关联的 SQL Server 数据库对 DataSet 执行的更改。
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);
daAuthors.Update(dsPubs, "m_task_001"); //数据适配器.Update()方法
//MessageBox.Show("数据库更新成功!");
//-------重新绑定dataGridView的数据源,以便重新显示-------
daAuthors.Fill(dsPubs, "m_task_001");
DataTable tblAuthors1;
tblAuthors1 = dsPubs.Tables["m_task_001"];
gridControl1.DataSource = tblAuthors1;
}
conn.Close();
conn.Dispose();
//MessageBox.Show("数据库连接已关闭");
CNT();//查询
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
C# 删除行
最新推荐文章于 2023-05-05 09:47:32 发布