ASP批量删除操作代码[全]

方法一:
<%
id=request.form("checkbox")
id=Split(id,",")
shu=0
for i=0 to UBound(id)
sql="select * from jiang_fname where id="&id(i)
set rs=conn.execute(sql)
if not rs.eof then
delete_file(rs("fname"))
end if
rs.close
set rs=nothing
sql="delete from jiang_fname where id="&id(i)      
conn.execute sql,shu1
shu=shu+1
next  

if shu>0 then
response.Write("")
else
response.write("")
end if
conn.close
set conn=nothing
%>


方法二:巧用in关键字实现数据的批量删除
managenews.asp


<%'数据库的连接文件我就不多说了%>


管理新闻






<%
set rs=server.createobject("adodb.recordset")
sql="select * from news order by addtime desc"
rs.open sql,conn,1,3
%>
<%if rs.eof then%>




没有新闻!

<%else%>














<%
do while not rs.eof
%>








<%
rs.movenext
loop
%>






新闻管理中心

删除框

新闻标题

发布时间

管理
" ID="Checkbox1">
<%If Len(rs("title"))<=30 Then%><%=rs("title")%><%else%>
<%=(Left(rs("title"),30))%>...
<%end if %>
<%=rs("addtime")%>">编 辑

选中所有的显示新闻 


<%end if%>
<%
set rs=nothing
conn.close
set conn=nothing
%>



delnews.asp文件

<%
arrdel=Request("id")
'Response.Write arrdel
sql="delete from news where id in ("&arrdel&")"
'Response.Write sql
conn.Execute sql
set conn=nothing
response.write""
response.end
%>








<%
set c.CreateObject("ADODB.Connection")
conn.c
conn.open
set com=server.CreateObject("ADODB.Command")
com.CommandText="delete from Portal_Articles where articleid="+ request.QueryString("articleId")
com.ActiveC
com.execute()
response.Redirect("admin_articles.asp")
'conn.execute("insert into portal_articles (title,author,catid) values('1314','test',5)")
'conn.close()
'response.Redirect("admin_articles.asp")
%>

''批量删除代码
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>


<%
'获取所有选中ID串
dim myIds
myIds=request.Form("chkId")
'分离Id到数组
dim ArticleIds
ArticleIds=split(myIds,", ")
'response.write(ubound(ArticleIds))
'连接数据库
'response.write(myIds)
set c.CreateObject("ADODB.Connection")
conn.c
conn.open
'''''''''''''''''''''''''''''''''''''''''''
'循环删除选中id
dim i
for i=0 to ubound(ArticleIds)
sqlstr="delete from portal_articles where ArticleId="+ArticleIds(i)
conn.execute(sqlstr)
'response.write(sqlstr+"
")
next
conn.close
if ubound(ArticleIds)>=0 then response.redirect("admin_articles.asp")'修正dreamweaver自动生成代码仍显示清除记录的Bug
%>










第一种

<%
id=request.form("checkbox")
    id=Split(id,",")
    shu=0
    for i=0 to UBound(id)
sql="select * from jiang_fname where id="&id(i)
set rs=conn.execute(sql)
if not rs.eof then
   delete_file(rs("fname"))
end if
rs.close
set rs=nothing
   sql="delete from jiang_fname where id="&id(i)    
   conn.execute sql,shu1
shu=shu+1
    next

    if shu>0 then
   response.Write("")
    else
   response.write("")
    end if
    conn.close
    set conn=nothing

%>
第二种
[ASP]通过复选框批量删除的处理方法:得到返回值的个数,通过一个for循环就可以解决问题..


解决方法:
<%
dim conn,connstr,rs,sqlstr,id
id=request("id")   '//id为复选框的NAME属性
set conn=server.createobject("ADOBC.connection")
connstr= "Driver={SQL Server};server=(local);uid=sa;pwd=;database=database_name;"
conn.open connstr
set rs=server.createobject("ADOBC.recordset")
on error resume next
for   i=0 to id.fields.count-1
sqlstr="delete * from table_name where id='"+Cstr(id.fields(i))+"';"
rs.execute(sql) '//或用下面语句:set rs=conn.execute(sqlstr)
next
rs.close
set rs=nothing
conn.close
set conn=nothing
Response.redirect("index.asp")   '//跳转到相关的页面
%>


''删除代码


<%
set conn=server.CreateObject("ADODB.Connection")
conn.connectionString=MM_conn_STRING
conn.open
set com=server.CreateObject("ADODB.Command")

com.CommandText="delete from Portal_Articles where articleid="+ request.QueryString("articleId")
com.ActiveConnection=conn
com.execute()
response.Redirect("admin_articles.asp")
'conn.execute("insert into portal_articles (title,author,catid) values('1314','test',5)")
'conn.close()
'response.Redirect("admin_articles.asp")
%>

''批量删除代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>



<%
'获取所有选中ID串
dim myIds
myIds=request.Form("chkId")

'分离Id到数组
dim ArticleIds
ArticleIds=split(myIds,", ")
'response.write(ubound(ArticleIds))
'连接数据库
'response.write(myIds)
set conn=server.CreateObject("ADODB.Connection")
conn.connectionString=MM_conn_STRING
conn.open
'''''''''''''''''''''''''''''''''''''''''''
'循环删除选中id
dim i
for i=0 to ubound(ArticleIds)
sqlstr="delete from portal_articles where ArticleId="+ArticleIds(i)
conn.execute(sqlstr)
'response.write(sqlstr+"
")
next
conn.close
if ubound(ArticleIds)>=0 then response.redirect("admin_articles.asp")'修正dreamweaver自动生成代码仍显示清除记录的Bug
%>


<%
关于上文的方法,通过实验居然行不通。问题出在那里我也没有搞清楚。不过找到了另一个方法来代替。我试验了一下,多复选框提交,返回的值是一个字符串。形如:(1,2,3,4,5,6...)之类。但是网上说可以通过数组下标的方式取得每一个的值,如文中所说的,通过name.fields(n)的方式取下应相的值。但是没有行通。最后我用的方法是先将返回的值折分将折分后的值赋给一个数组变量。然后通过取数组最大的可用下标来取得所要的值。其中要用到两个函数:一个:split("","")是用第二个参数来折分第一个参数。二个:UBound(数组)。返回该数组的最大可用下标。好了不多说了,要上班了,先把代码帖上来,以后再来分折了。

代码:
if trim(EmailTitle)="" then
MessageBox("邮件发送失败!请填写你要发送的邮件的标题!")
else
if trim(Message)="" then
MessageBox("邮件发送失败!请填写你要发送的邮件的内容!")
else
dim EMail_Add_Array,EMail_Add_Array_Count
EMail_Add_Array = split(Student_Id_Str,",")
EMail_Add_Array_Count = UBound(EMail_Add_Array)
if EMail_Add_Array_Count<0 then
MessageBox("邮件发送失败!请选择接收邮件的用户!")
else
dim rs_EMail_Add,sql_EMail_Add
set rs_EMail_Add= Server.CreateObject("ADODB.Recordset")
sql_EMail_Add="select * from E_Mail where MailID IS NULL"
rs_EMail_Add.open sql_EMail_Add,conn,1,3
dim m_Count
m_Count=0
for i=0 to EMail_Add_Array_Count
rs_EMail_Add.addnew
rs_EMail_Add("FromUid")=UserId
rs_EMail_Add("ToUid")=EMail_Add_Array(i)
rs_EMail_Add("TiTle")=EMailTitle
rs_EMail_Add("Detail")=Message
rs_EMail_Add("IsRead")=0
if SaveMail then
rs_EMail_Add("SendDel")=1
else
rs_EMail_Add("SendDel")=0
end if
rs_EMail_Add("RecvDel")=0
rs_EMail_Add("Writetime")=Now
rs_EMail_Add.update
m_Count=m_Count+1
next
MessageBox("恭喜!!您已经成功的发送了" & m_Count &"封邮件!")
rs_EMail_Add.close
set rs_EMail_Add=Nothing
Class_Id = ""
Student_Id_Str = 0
EmailTitle= ""
Message = ""
SaveMail = 0
end if
end if
end if
%>


上一篇: asp返回上一页
下一篇: 成功的ASP批量删除修改操作实例
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1122
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 300 字 | UBB代码 开启 | [img]标签 关闭