private void button7_Click(object sender, EventArgs e)
        {  dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);            SqlConnection conn = new SqlConnection();
            
                int a=(int)dataGridView1.SelectedRows[0].Index;
               
            
            try
                { 
                String strCon = "Data Source=(local); Initial catalog=lxz; Integrated Security=true";
                conn.ConnectionString = strCon;
                conn.Open();
                MessageBox.Show("数据库连接成功");
                SqlCommand command = new SqlCommand();
                command.CommandText = "DELETE * FROM [table] WHERE 条件 ";
                command.Connection = conn;
                SqlDataReader reader = command.ExecuteReader();
                           }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);            }
            finally
            {
                conn.Close();
            }
            
        }
这是我要删除dataGridView表中记录的删除按钮的代码
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);这句代码只能删除表中的数据,但不能删除数据库中的数据,而DELETE * FROM [table] WHERE 条件 的这个条件怎么写啊,怎样可以删除数据库中的数据。
本人刚学,请高手详细说明

解决方案 »

  1.   

     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlStr = "delete from Employee where ID=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) + "";
            Common.ExecuteSql(sqlStr);
            bind();
        }
      

  2.   

    Common.ExecuteSql(sqlStr); 
    还有这种用法?学习了,但MSDN里找不到啊
      

  3.   

    1、Remove或RemoveAt是不行的!!!!
    因为这是直接将DataTable的一行删除掉,而非将行标记为“已删除”,既然没有任何标记,因此在写入数据库时也就没做任何动作,因此要用Delete才行2、1楼的做法应该是可行的,(除了Common.ExecuteSql(sqlStr)这一句我没看懂,但意思是很明白的,在调用RemoveAt时将执行delete SQL语句。)但是这样的话就不能够撤销了,想想如果你点了删除,过后再点取消的话,数据已经在数据库里被删掉了......3、command.CommandText = "DELETE * FROM [table] WHERE 条件 "; 
       这句有语病吧~~~ 如果你是想RemoveAt后,再直接运行delete 语句删除记录的话再说也不用command.ExecuteReader啊
      

  4.   

    呵呵
    因为我刚学,大部分代码是从书上和网上找的
    我现在就是想点击一下按钮,把dataGridView中的一条记录不光在表中删出,也在数据库中删除
    各位高手能不能写的详细写,或者帮我把上面的代码给一下。不胜感激!!!
      

  5.   

    private void button7_Click(object sender, EventArgs e) 
            {  dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);             SqlConnection conn = new SqlConnection(); 
                
                    int a=(int)dataGridView1.SelectedRows[0].Index; 
                  
                
                try 
                    { 
                    String strCon = "Data Source=(local); Initial catalog=lxz; Integrated Security=true"; 
                    conn.ConnectionString = strCon; 
                    conn.Open(); 
                    //MessageBox.Show("数据库连接成功"); 
                    SqlCommand command = new SqlCommand(); 
                    command.CommandText = "DELETE [table] WHERE 条件 "; 
                    command.Connection = conn; 
                    command.ExecuteNoQuery(); 
                              } 
                catch (SqlException ex) 
                { 
                    MessageBox.Show(ex.Message);             } 
                finally 
                { 
                    conn.Close(); 
                } 
                
            } 
      

  6.   

    楼上的高手,我就是想在dataGridView表中,点击这行,然后点击删除按钮,就会把这行在库中删掉
    我就是不知道 command.CommandText = "DELETE [table] WHERE 条件 "; 
    的这个条件具体要怎么写
    帮帮忙吧!!!
      

  7.   

     private void button7_Click(object sender, EventArgs e)
            {           
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);            SqlConnection conn = new SqlConnection();          
                string xh =dataGridView1.CurrentRow.Cells[0].Value.ToString();
                Console.WriteLine(xh);
                try
                {
                    String strCon = "Data Source=(local); Initial catalog=lxz; Integrated Security=true";
                    conn.ConnectionString = strCon;
                    conn.Open();
                                   SqlCommand command = new SqlCommand();
                    command.CommandText = "DELETE lxz WHERE 序号="+xh ;
                    command.Connection = conn;
                    command.ExecuteNonQuery();
                    MessageBox.Show("数据库记录删除成功!"); 
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);            }
                finally
                {
                    conn.Close();
               }
    }
    ========================================================
    按照楼上高手的指点,我现在已经可以在dataGridView中删除数据库中的数据了,但是我在删除表中第一条记录时,红字处总是报错,只要不是删除第一条记录,执行都正常。为什么?我把红字处的代码删除后,删第一行就不报错,但是只是把库中的数据删掉了,表还是能看见。有什么办法可以把表中的也删除而不报错的么?
    或者重新刷新一下dataGridView的内容,让他重新读数据库的数据也行
      

  8.   

    private void button7_Click(object sender, EventArgs e) 
            {           
               SqlConnection conn = new SqlConnection();           
                string xh =dataGridView1.CurrentRow.Cells[0].Value.ToString(); 
                Console.WriteLine(xh); 
                try 
                { 
                    String strCon = "Data Source=(local); Initial catalog=lxz; Integrated Security=true"; 
                    conn.ConnectionString = strCon; 
                    conn.Open(); 
                                  SqlCommand command = new SqlCommand(); 
                    command.CommandText = "DELETE lxz WHERE 序号="+xh ; 
                    command.Connection = conn; 
                    command.ExecuteNonQuery(); 
                    MessageBox.Show("数据库记录删除成功!"); 
                } 
                catch (SqlException ex) 
                { 
                    MessageBox.Show(ex.Message);             } 
                finally 
                { 
                    conn.Close(); 
              } 
     dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);