博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataSet之增删改查操作(DataGridView绑定)
阅读量:6932 次
发布时间:2019-06-27

本文共 5561 字,大约阅读时间需要 18 分钟。

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];      }

 

你可能感兴趣的文章
Java学习之深拷贝浅拷贝及对象拷贝的两种方式
查看>>
如何根据动态SQL代码自动生成DTO
查看>>
html input="file" 浏览时只显示指定文件类型 xls、xlsx、csv
查看>>
Android Export aborted because fatal error were fo
查看>>
在window平台下模拟Liunx使用GCC环境进行编译C的SO库。
查看>>
原来一直纠结MQ的用法,今天看到了一个最经典的例子。
查看>>
向量空间
查看>>
[日推荐]『共享记账』你的私人会计师!
查看>>
Resource is out of sync with the file system的解决办法
查看>>
交叉编译openssl不修改Makefile的方法
查看>>
linux 常用流量查看命令
查看>>
【React Native开发】React Native库版本升级(Upgrading)与降级讲解
查看>>
关于MPAndroidChart柱状图左右滑动
查看>>
linux安装tomcat
查看>>
VMware ESXi Windows虚拟机磁盘扩展小结
查看>>
Linux常用命令
查看>>
ios自动打包脚本,融合xctool、xcodebuild、xcrun
查看>>
关于Android调用安装的各种技巧
查看>>
方便的将数字转成字符串类型并在前面补0
查看>>
mysql主从复制
查看>>