Chinaunix首页 | 论坛 | 博客
  • 博客访问: 510604
  • 博文数量: 88
  • 博客积分: 2256
  • 博客等级: 大尉
  • 技术积分: 921
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-08 23:20
个人简介

积硅步,行千里

文章分类

全部博文(88)

文章存档

2019年(5)

2018年(1)

2016年(15)

2015年(23)

2013年(3)

2012年(6)

2011年(3)

2010年(22)

2009年(10)

我的朋友

分类: C#/.net

2015-10-28 17:07:44


点击这里收缩:

  1. private void DeleteBtn_Click(object sender, EventArgs e)
  2.         {
  3.             //string dbconn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;(方法一)
  4.             string dbconn = configxml.xmlconn.readxml(); //方法二
  5.             SqlConnection sqlcon = new SqlConnection(dbconn);

  6.             if (dataGridView1.DataSource == null || dataGridView1.CurrentRow == null)
  7.             {
  8.                    return;
  9.             }
  10.             else
  11.             {
  12.                 if (this.dataGridView1.SelectedRows.Count > 0)
  13.                 {
  14.                     DialogResult dr = MessageBox.Show("确定删除选中的记录 ", "提示",MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  15.                     if (dr == DialogResult.OK)
  16.                     {
  17.                         try
  18.                         {
  19.                             sqlcon.Open();; //打开数据库连接
  20.                             foreach (DataGridViewRow row in this.dataGridView1.SelectedRows) //遍历所选中的dataGridView记录行
  21.                             {
  22.                                 string strName = row.Cells[1].Value.ToString(); //取dataGridView1中的第二列的值
  23.                                 string sql = string.Format("delete from T_users where username='{0}'", strName); //SQL语句来执行要求操作
  24.                                 SqlCommand cmd = new SqlCommand(sql, sqlcon); //执行sqlcommand的数据连接
  25.                                 cmd.ExecuteNonQuery(); //执行删除操作
  26.                                 cmd.Dispose();
  27.                             }
  28.                         }
  29.                         catch (Exception ex)
  30.                         {
  31.                             MessageBox.Show(ex.ToString(), "提示");
  32.                         }
  33.                         finally
  34.                         {
  35.                             sqlcon.Close(); //关闭数据库连接
  36.                         }
  37.                         this.ButtonFresh_Click(this.t_usersTableAdapter.Fill(this.misds.T_users),null);
  38.                         //删除后执行刷新操作ButtonFresh是刷新按钮的名字,刷新方法在此就不写了。
  39.                     }
  40.                     else
  41.                     {
  42.                         return;
  43.                     }
  44.                 }
  45.             }
  46.         }
  47. #endregion

阅读(2123) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~