网站中的批量删除很常见,特别是对在gridview中做批量删除。我们一般的做法是循环到勾选的就调用过程直接删除。如果是上百上千的数据要删除,那就得调用试行上百次。有人认为直接试行语句,何必那么麻烦但是我们可以对它处理的完善点,无论你选多少要删除的数据,只需调用试行一次。
代码
代码
代码
底下写的存储过程删除语句与上图无关,上图是常见的在gridview中做批量删除。使用存储过程:


<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> -- -------------------------------------------------------------------------------------
/*
*作 者:lin sen
*功能说明:动态构建SQL语句之删除
*编写日期:2010年9月27日
*
*/
-- -------------------------------------------------------------------------------------
drop procedure proc_DeleteMessage
go
create procedure proc_DeleteMessage
(
@condition varchar ( 500 ) -- 删除条件(多个)
)
as
begin
declare @sql varchar ( 200 )
-- 动态构建删除语句
select @sql = ' Delete from MessageInfo where ' + @condition
-- 试行语句
exec ( @sql )
end
go
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> -- -------------------------------------------------------------------------------------
/*
*作 者:lin sen
*功能说明:动态构建SQL语句之删除
*编写日期:2010年9月27日
*
*/
-- -------------------------------------------------------------------------------------
drop procedure proc_DeleteMessage
go
create procedure proc_DeleteMessage
(
@condition varchar ( 500 ) -- 删除条件(多个)
)
as
begin
declare @sql varchar ( 200 )
-- 动态构建删除语句
select @sql = ' Delete from MessageInfo where ' + @condition
-- 试行语句
exec ( @sql )
end
go
在SQL查询分析器上调用该过程:(传入的条件是唯一标识列名和所选中的值)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> exec proc_DeleteMessage ' MessageID=240 or MessageID=241 or MessageID=242... '
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> exec proc_DeleteMessage ' MessageID=240 or MessageID=241 or MessageID=242... '
在SQL调用看不太清楚,我们来看下Web中的前台调用与试行。


<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> // 删除按钮单击事件
protected void LBtn_Del_Click( object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
for ( int i = 0 ; i < GV_class.Rows.Count; i ++ )
{
CheckBox checkbox = (CheckBox)GV_class.Rows[i].FindControl( " checkbox " );
if (checkbox.Checked == false )
{
lab_Note.Text = " 请选择要删除信息 " ;
lab_Note.Style.Add( " color " , " red " );
}
else
{
MessageModel.C_ID = Int32.Parse(GV_class.Rows[i].Cells[ 3 ].Text.Trim()); // 选中的唯一标识列值
sb.Append( " MessageID= " );
sb.Append(MessageModel.C_ID);
sb.Append( " or " );
}
}
sb.Append( " MessageID=null " );
MessageModel.SQLSTR = sb.ToString(); // 动态的条件语句传给实体
int j = DeleteClass(MessageModel);
// .....
}
///
/// 删除信息
///
///
///
public int DeleteClass(MessageModel MeModel)
{
int rowsAffected;
SqlParameter[] parameter = { new SqlParameter( " @sqlstr " , SqlDbType.Int) };
parameter[ 0 ].Value = MeModel.SQLSTR;
DbHelperSQL.RunIntProcName( " proc_DeleteMessage " , out rowsAffected, parameter);
return rowsAffected;
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> // 删除按钮单击事件
protected void LBtn_Del_Click( object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
for ( int i = 0 ; i < GV_class.Rows.Count; i ++ )
{
CheckBox checkbox = (CheckBox)GV_class.Rows[i].FindControl( " checkbox " );
if (checkbox.Checked == false )
{
lab_Note.Text = " 请选择要删除信息 " ;
lab_Note.Style.Add( " color " , " red " );
}
else
{
MessageModel.C_ID = Int32.Parse(GV_class.Rows[i].Cells[ 3 ].Text.Trim()); // 选中的唯一标识列值
sb.Append( " MessageID= " );
sb.Append(MessageModel.C_ID);
sb.Append( " or " );
}
}
sb.Append( " MessageID=null " );
MessageModel.SQLSTR = sb.ToString(); // 动态的条件语句传给实体
int j = DeleteClass(MessageModel);
// .....
}
///
/// 删除信息
///
///
///
public int DeleteClass(MessageModel MeModel)
{
int rowsAffected;
SqlParameter[] parameter = { new SqlParameter( " @sqlstr " , SqlDbType.Int) };
parameter[ 0 ].Value = MeModel.SQLSTR;
DbHelperSQL.RunIntProcName( " proc_DeleteMessage " , out rowsAffected, parameter);
return rowsAffected;
}
后续完善:(避免大家犯同样错误)
由于个人考虑和分析的不周,此方法存在很多问题,感谢几位的评论和建议;使本人受益匪浅!底下是存在的问题:
1、有人提出用in比or效率更好?
2、用存储过程后避免在程序上拼接SQL语句,或者直接在程序上拼接。
3、跟普通的删除没咋区别,是否只删除一页?
本人还是用存储过程拼接解法吧,以下是修改后的:


<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> -- -------------------------------------------------------------------------------------
/*
*作 者:lin sen
*功能说明:动态构建SQL语句之删除
*编写日期:2010年9月27日
*
*/-- -------------------------------------------------------------------------------------
drop procedure proc_DeleteMessage
go
create procedure proc_DeleteMessage
(
@tablename varchar ( 100 ), -- 表名
@colname varchar ( 100 ), -- 列名
@condition varchar ( 500 ) -- 列值
)
as
begin
declare @sql varchar ( 500 )
select @sql = ' delete from ' + @tablename + ' where ' + @colname + ' in( ' + @condition + ' ) '
print @sql
exec ( @sql )
end
go
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> -- -------------------------------------------------------------------------------------
/*
*作 者:lin sen
*功能说明:动态构建SQL语句之删除
*编写日期:2010年9月27日
*
*/-- -------------------------------------------------------------------------------------
drop procedure proc_DeleteMessage
go
create procedure proc_DeleteMessage
(
@tablename varchar ( 100 ), -- 表名
@colname varchar ( 100 ), -- 列名
@condition varchar ( 500 ) -- 列值
)
as
begin
declare @sql varchar ( 500 )
select @sql = ' delete from ' + @tablename + ' where ' + @colname + ' in( ' + @condition + ' ) '
print @sql
exec ( @sql )
end
go
这边的话调用的时候还要处理,就是你传入的列值如果是字符串的,那么应该是in('','',''....),整形的是:in(, , ,....).
这样的话就解决1,2问题。对于3,这个做法是对在GridView中做批量选择删除,只要你有选中的都会删除。像上面前台
我们一般的做法是循环遍历一次调用一次过程,所以采用先循环遍历一次后,只调用一次就全部删除。
谢谢大家的评论和建议!
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-683055/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-683055/