请问大家是如何一次删除一千条数据的?
如题。
打个比方:页面上用 CheckBoxList 绑定了当前所有的用户,我勾选中了其中的1000个用户,然后按下删除按钮,大家通常都是怎样去删除掉这1000个用户的呢?请阐明你的思路,最好列出简单的代码,我使用的是 .net2.0 + Sql Server2000 ,谢谢。
[解决办法]
如果不用考虑事务的问题,也就是说就删到第101个出错,前100个还是删除成功的话
我一般用拼接SQL语句,然后执行一次 cmd.ExecuteNonQuery();全部删除
string whereClause = string.Empty; foreach (遍历CheckBoxList) { if (选中) { if (whereClause == string.Empty) whereClause += "'" + 取值 + "'"; else whereClause += ",'" + 取值 + "'"; } } if (whereClause != string.Empty) { SqlConnection cn = new SqlConnection(...); string strSQL = "delete from mytable where id in (" + whereClause + ")"; SqlCommand cmd = new SqlCommand(strSQL, cn); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }
[解决办法]