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, "提示");
                 
  • 相关阅读:
    Selenium简单测试页面加载速度的性能(Page loading performance)
    Selenium Page object Pattern usage
    Selenium如何支持测试Windows application
    UI Automation的两个成熟的框架(QTP 和Selenium)
    分享自己针对Automation做的两个成熟的框架(QTP 和Selenium)
    敏捷开发中的测试金字塔(转)
    Selenium 的基础框架类
    selenium2 run in Jenkins GUI testing not visible or browser not open but run in background浏览器后台运行不可见
    eclipse与SVN 结合(删除SVN中已经上传的问题)
    配置Jenkins的slave节点的详细步骤适合windows等其他平台
  • 原文地址:https://www.cnblogs.com/endv/p/5352429.html
Copyright © 2011-2022 走看看