zoukankan      html  css  js  c++  java
  • ListView控件绑定DataSet

    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, "提示");
                 
  • 相关阅读:
    PyDev for eclipse 插件下载地址
    Impala SQL 语言元素(翻译)[转载]
    oracle9i-11.2安装包及补丁包下载链接
    oracle link的创建过程
    Oracle 查询历史数据(转帖)
    Alpha、Beta、RC、GA版本的区别
    oracle wm_concat(column)函数的使用
    Quest.Central.for.DB2.v5.0.2.4下载地址
    Hadoop 管理工具HUE配置-初始配置
    maven编译下载源码
  • 原文地址:https://www.cnblogs.com/endv/p/5352429.html
Copyright © 2011-2022 走看看