博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ListView控件绑定DataSet
阅读量:6544 次
发布时间:2019-06-24

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

DataSet数据集,数据缓存在客户端内存中,支持断开式连接.

  在对DataSet做操作的时候,首先一定要修改其行的状态,然后执行SqlDataAdapter的Update方法,Update方法根据其行的状态,做相应的SelectCommand、DeleteCommand、UpdateCommand、InsertCommand操作. 
 
一,ListView控件绑定DataSet之操作: 
1
)查找操作
using (SqlConnection con = new SqlConnection(constring))
            {
                //con.Open();
                ds = new DataSet();
                StringBuilder sb = new StringBuilder();
                sb.Append("select stucode,spcode,ctcode,stunits from stunits");
                dapt = new SqlDataAdapter(sb.ToString(), con);
                try
                {
                    dapt.Fill(ds, "stunit");
                    //循环遍历表中的行
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ListViewItem item = this.listView1.Items.Add(dr[0].ToString().Trim());
                        item.SubItems.Add(dr[1].ToString().Trim());
                        item.SubItems.Add(dr[2].ToString().Trim());
                        item.SubItems.Add(dr[3].ToString().Trim()); 
                    }
                }
                catch (SystemException ex)
                {
                    MessageBox.Show(ex.Message);
                    //MessageBox.Show("出错");
                }
            }
2)修改操作
        private void UpdateDatabase()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("update stunits set stucode=@stucode,spcode=@spcode,ctcode=@ctcode,stunits=@stunits where stucode='" + this.Listv.SelectedItems[0].Text + "' ");
            using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=system;database=jtyyhis"))
            {
 
                SqlCommand cmd = new SqlCommand(sb.ToString(), con);
                this.dapt = new SqlDataAdapter();
                dapt.UpdateCommand = cmd;
                dapt.UpdateCommand.Parameters.Add("@stucode", SqlDbType.VarChar, 10);
                dapt.UpdateCommand.Parameters.Add("@spcode", SqlDbType.VarChar, 10);
                dapt.UpdateCommand.Parameters.Add("@ctcode", SqlDbType.VarChar, 10);
                dapt.UpdateCommand.Parameters.Add("@stunits", SqlDbType.VarChar, 40);
                dapt.UpdateCommand.Parameters[0].Value = this.textBox1.Text;
                dapt.UpdateCommand.Parameters[1].Value = this.textBox2.Text;
                dapt.UpdateCommand.Parameters[2].Value = this.textBox3.Text;
                dapt.UpdateCommand.Parameters[3].Value = this.textBox4.Text;
 
                 
//得到当前行的索引
                int index = this.Listv.SelectedIndices[0];
                //修改行状态,使其状态成为Modified
                ds.Tables[0].Rows[index].SetModified();
                if (ds.HasChanges(DataRowState.Modified))
                {
                    try
                    {
                        dapt.Update(ds, "stunit");
                        MessageBox.Show("修改成功");
                    }
                    catch (DBConcurrencyException ex1)
                    {
                        MessageBox.Show(ex1.Message + " " + ex1.StackTrace);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
 
                }
            }
3)增加操作
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == string.Empty)
            {
                MessageBox.Show("代码不能为空", "提示");
            }
            else
            {
                using (SqlConnection con = new SqlConnection(constring))
                {
                    //con.Open();
                    StringBuilder sb = new StringBuilder();
                    sb.Append("insert into stunits(stucode,spcode,ctcode,stunits) values(@dm,@pym,@dym,@stunit)");
                    dapt = new SqlDataAdapter();
                    //产生一个新行
                   
 DataRow dr = ds.Tables[0].NewRow();
                    dr[0] = this.textBox1.Text;
                    dr[1] = this.textBox2.Text;
                    dr[2] = this.textBox3.Text;
                    dr[3] = this.textBox4.Text;
                    //将新行添加到ds.Tables[0].Rows中
                    ds.Tables[0].Rows.Add(dr);
                    SqlCommand cmd = new SqlCommand(sb.ToString(), con);
                    cmd.Parameters.Add("@dm", SqlDbType.VarChar, 10, "stucode");
                    cmd.Parameters.Add("@pym", SqlDbType.VarChar, 20, "spcode");
                    cmd.Parameters.Add("@dym", SqlDbType.VarChar, 20, "ctcode");
                    cmd.Parameters.Add("@stunit", SqlDbType.VarChar, 40, "stunits");
                    dapt.InsertCommand = cmd;
                    //dapt.InsertCommand = new SqlCommand(sb.ToString(), con);
                    //dapt.InsertCommand.Parameters.Add("@dm", SqlDbType.Int,10,"stucode");
                    //dapt.InsertCommand.Parameters.Add("@pym", SqlDbType.VarChar,20,"spcode");
                    //dapt.InsertCommand.Parameters.Add("@dym", SqlDbType.VarChar,20,"ctcode");
                    //dapt.InsertCommand.Parameters.Add("@stunit",SqlDbType.VarChar,40,"stunits");
                    try
                    {
                        dapt.Update(ds, "stunit");
                        MessageBox.Show("添加成功");
                    }
                    catch (ArgumentNullException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    catch (SqlException ex1)
                    {
                        MessageBox.Show(ex1.Message);
                    }
 
                }
 4
)删除操作
     private void btnDele_Click(object sender, EventArgs e)
        {
            //ds = new DataSet();
            using (SqlConnection con = new SqlConnection(constring))
            {
                //con.Open();
                StringBuilder sb = new StringBuilder();
                sb.Append("delete stunits where stucode='" + this.listView1.SelectedItems[0].Text + "'");
                SqlCommand cmd = new SqlCommand(sb.ToString(), con);
                dapt = new SqlDataAdapter();
                dapt.DeleteCommand = cmd;
                
int index = this.listView1.SelectedIndices[0];//得到选中行的索引
                ds.Tables["stunit"].Rows[index].Delete();//改变行的状态
                //try
                //{
                //    int Count = cmd.ExecuteNonQuery();
                //    if (Count > 0) MessageBox.Show("删除成功", "提示");
                //}
                //catch (SqlException ex)
                //{
                //    MessageBox.Show(ex.Message);
                //}
                if (ds.HasChanges(DataRowState.Deleted))
                {
                    try
                    {
                        dapt.Update(ds, "stunit");
                        MessageBox.Show("删除成功", "提示");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message, "提示");
             

转载地址:http://jhodo.baihongyu.com/

你可能感兴趣的文章
微软邮件系统Exchange 2013系列(七)创建发送连接器
查看>>
程序员杂记系列
查看>>
【树莓派】制作树莓派所使用的img镜像(一)
查看>>
理解网站并发量
查看>>
spring整合elasticsearch之环境搭建
查看>>
TensorFlow 架构与设计-编程模型【转】
查看>>
如何运行Struts2官网最新Demo?
查看>>
'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
查看>>
XDebug 教程
查看>>
js 去html 标签
查看>>
好久不见
查看>>
小tips:JS中的children和childNodes
查看>>
二叉树的遍历
查看>>
Oracle的FIXED_DATE参数
查看>>
PostgresSQL中的限制和级联删除
查看>>
NDK配置
查看>>
(转)@ContextConfiguration注解说明
查看>>
docker in centos error
查看>>
c# 线程同步: 详解lock,monitor,同步事件和等待句柄以及mutex
查看>>
[置顶] ※数据结构※→☆线性表结构(queue)☆============队列 顺序存储结构(queue sequence)(八)...
查看>>