C# 删除行

//删除一行
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());
    }
   
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑先生Ethan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值