随笔 - 30  文章 - 3  评论 - 5  阅读 - 8535

datagridview数据绑定操作数据库实现增删改查

DataSet数据集,数据缓存在客户端内存中,支持断开式连接.DataGridView控件绑定DataSet时,它自动的改变的DS的行的状态,而且在做增删改查的时候,可以借助SqlCommandBuilder类来完成.

  SqlCommandBuilder必须执行SELECT命令来检索元数据,所以它要求多往返服务器一次,从而增加了应用程序的开销,而且操作的表必须要有主键约束。优点是自动建立insertcommand等命令
1,添加操作
  private void button2_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(connstring))
            {
                con.Open();
                dapt = new SqlDataAdapter("select stucode,spcode,ctcode,stunits from stunits", con);
                SqlCommandBuilder sdb = new SqlCommandBuilder(dapt);
                DataSet ds1 = ds.GetChanges();
                if (ds.HasChanges())
                {
                    try
                    {
                        dapt.Update(ds, "stunit");
                        MessageBox.Show("保存成功", "提示");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
               
            }
            this.dataGridView1.AllowUserToAddRows =false;
        }
2)删除操作(选中整行)
   private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(connstring))
            {
                //con.Open();
                StringBuilder sb  =new StringBuilder();
                sb.Append("delete stunits where stucode='"+this.dataGridView1.SelectedCells[0].Value.ToString()+"'");
                SqlCommand cmd = new SqlCommand(sb.ToString(), con);
                dapt.DeleteCommand = cmd;
                int rowindex = this.dataGridView1.CurrentCell.RowIndex;
                ds.Tables["stunit"].Rows[rowindex].Delete();//修改行的状态
               // string celltext = this.dataGridView1.SelectedCells[rowindex].Value.ToString();
               // MessageBox.Show(celltext);
                if(ds.HasChanges(DataRowState.Deleted))
                {
                     try
                    {
                        dapt.Update(ds, "stunit");
                        MessageBox.Show("删除成功", "提示");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
2)删除操作(单击第一个单无格删除)
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >= 0)
            {
                using (conn = new SqlConnection(con))
                {
                    conn.Open();
                    StringBuilder sb = new StringBuilder();
                    sb.Append("delete zy_bbxmxx where 代码='" + this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()+"'");
                    //MessageBox.Show(sb.ToString());
                    dapt.DeleteCommand = new SqlCommand(sb.ToString(), conn);
                    DialogResult answer = MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (answer == DialogResult.Yes)
                    {
                        try
                        {
                            ds.Tables[0].Rows[e.RowIndex].Delete();//改变行的状态
                            dapt.Update(ds, "zy_bbxx");
                            MessageBox.Show("删除成功");
                        }
                        catch (SqlException ex)
                        {
                            
                            MessageBox.Show(ex.Message);
                        }
                       
                    }
                }
            }
        } 
        
3)修改操作
 private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(connstring))
            {
                con.Open();
                //DataSet ds1 = ds.GetChanges();
                dapt = new SqlDataAdapter("select stucode,spcode,ctcode,stunits from stunits", con);
                SqlCommandBuilder sdb = new SqlCommandBuilder(dapt);
                if (ds.HasChanges())
                {
                    try
                    {
                        dapt.Update(ds, "stunit");
                        MessageBox.Show("修改成功","提示");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
[转载]DataSet之增删改查操作(DataGridView绑定)
或者
 private void btnEdit_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn=new SqlConnection(con))
            {
                DataSet ds1 = ds.GetChanges();//得到修改后的DataSet
                if (ds1 != null)
                {
                    try
                    {
                        dapt = new SqlDataAdapter("select * from zy_bbxmxx", conn);
                        SqlCommandBuilder sb = new SqlCommandBuilder(dapt);        
                        dapt.Update(ds,"zy_bbxx");
                        MessageBox.Show("修改成功");
                     
                    }
                    catch (ArgumentNullException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                
                }
            }  
        }
4)查询操作
  private void textBox5_TextChanged(object sender, EventArgs e)
        {
            this.dataGridView2.DataSource = ds.Tables[1];
                DataView dv = ds.Tables[1].DefaultView;
                dv.RowFilter = "stucode like   '" + '%' + this.textBox5.Text + '%' + "'";
                this.dataGridView2.DataSource = ds.Tables[1];
      }
posted on   心有猛虎细嗅蔷薇  阅读(1293)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示