日期:2014-05-18  浏览次数:20759 次

批量删除问题
protected   void   btnDelete_Click(object   sender,   EventArgs   e)
        {
                Db   db   =   new   Db();
                SqlConnection   conn   =   db.GetConn();
                SqlCommand   com;
                for   (int   i   =   0;   i   <=   GridView1.Rows.Count   -   1;   i++)
                {
                        CheckBox   cbox   =   (CheckBox)GridView1.Rows[i].FindControl( "ckbChoose ");
                        Label   lblID   =   (Label)GridView1.Rows[i].FindControl( "Label1 ");
                        if   (cbox.Checked   ==   true)
                        {
                                string   str   =   "delete     from   tb_Department   where   ID= ' "   +   Convert.ToInt32   (lblID.Text)   + " ' "   ;
                                com   =   new   SqlCommand(str,   conn);
                                conn.Open();
                                com.ExecuteNonQuery();
                                conn.Close();
                        }
                }
以上是我的代码,可是单击删除按钮时没一点反应啊,也没有错误提示.拜托各位,帮忙看一下.

------解决方案--------------------
protected void btnDelete_Click(object sender, EventArgs e)
{
Db db = new Db();
SqlConnection conn = db.GetConn();
SqlCommand com;
string str=String.Empty;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl( "ckbChoose ");
Label lblID = (Label)GridView1.Rows[i].FindControl( "Label1 ");
if (cbox.Checked == true)
{
str += "delete from tb_Department where ID= ' " + Convert.ToInt32 (lblID.Text) + " ' " ;
str += ";\r\n ";

}
com = new SqlCommand(str, conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}

改成上面的试一下

------解决方案--------------------
调试跟踪一下 看怎么执行了
不过楼主这样的 批量删除 要把数据库服务器累的不行了
连接关闭太频繁了
------解决方案--------------------
看你的代码应该没有问题,不过,似乎多行删除应该加入事务处理的,而且要有TRY CATCH,并且在DATABASE中删除了一行后可以将该行从DATAGRIDVIEW中REMOVE,这样你看到的与数据库操作的将同步.当然,也可以在循环删除后重新绑定数据源.