Discuz教程网

ASP下批量删除数据的两种方法

[复制链接]
authicon dly 发表于 2011-9-14 09:05:26 | 显示全部楼层 |阅读模式
方法一:
  1. id=request.form("checkbox")
  2. id=Split(id,",")
  3. shu=0
  4. for i=0 to UBound(id)
  5. sql="select * from jiang_fname where id="&id(i)
  6. set rs=conn.execute(sql)
  7. if not rs.eof then
  8. delete_file(rs("fname"))
  9. end if
  10. rs.close
  11. set rs=nothing
  12. sql="delete from jiang_fname where id="&id(i)
  13. conn.execute sql,shu1
  14. shu=shu+1
  15. next

  16. if shu>0 then
  17. response.Write("<script>alert('删除成功');location.href='"&url&"';</script>")
  18. else
  19. response.write("<Script>alert('删除失败');javascript: history.back();</script>")
  20. end if
  21. conn.close
  22. set conn=nothing
复制代码



方法二:巧用in关键字实现数据的批量删除
managenews.asp
  1. <!--#include file="conn.asp"-->
  2. <%'数据库的连接文件我就不多说了%> <html>
  3. <head>
  4. <title>管理新闻</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  6. <link rel="stylesheet" href="../index/style.css" type="text/css">
  7. <script>
  8. function del () //用于判断记录有没有选中的函数
  9. {
  10. var flag=true;
  11. var temp="";
  12. var tmp;
  13. if((document.form1.answer.length+"")=="undefined") {tmp=1}else{tmp=document.form1.answer.length}
  14. if (tmp==1){
  15. if (document.form1.answer.checked){
  16. flag=false;
  17. temp=document.form1.answer.value
  18. }
  19. }else{
  20. for (i=0;i<document.form1.answer.length;i++) {
  21. if (document.form1.answer[i].checked){
  22. if (temp==""){
  23. flag=false;
  24. temp=document.form1.answer[i].value
  25. }else{
  26. flag=false;
  27. temp = temp +","+ document.form1.answer[i].value
  28. }
  29. }
  30. }
  31. }
  32. if (flag){ alert("对不起,你还没有选择!")}
  33. else{ name=document.form1.name.value
  34. //alert(name)
  35. if (confirm("确实要删除?")){
  36. window.location="delnews.asp?id=" + temp;
  37. }
  38. }
  39. return !flag;
  40. }
  41. </script>
  42. </head>
  43. <body>
  44. <script language=Javascript>
  45. function checkall(all)//用于判断全选记录的函数
  46. {
  47. var a = document.getElementsByName("answer");
  48. for (var i=0; i<a.length; i++) a[i].checked = all.checked;
  49. }
  50. </script>
  51. <%
  52. set rs=server.createobject("adodb.recordset")
  53. sql="select * from news order by addtime desc"
  54. rs.open sql,conn,1,3 %>
  55. <% if rs.eof then %>
  56. <table width="50%" border="0" align="center" ID="Table2">
  57. <tr>
  58. <td align="center">
  59. 没有新闻!
  60. </tr>
  61. </table>
  62. <% else %>
  63. <form method="POST" id=form1 name=form1>
  64. <table width="90%" border="0" align="center" class="tabDocborder" ID="Table3">
  65. <tr>
  66. <td>
  67. <table width="80%" align="center" id=TabDocMain border='1' cellspacing='0' cellpadding='0' bordercolorlight='#82b4dd' bordercolor='#b6d3eb'     class="TabDocMain">
  68. <thead>
  69. <tr>
  70. <td colspan="7" align="center">
  71. 新闻管理中心
  72. </td>
  73. </tr>
  74. </thead>
  75. <tbody>
  76. <tr>
  77. <td align=center>
  78. 删除框
  79. </td>
  80. <td align=center>
  81. 新闻标题
  82. </td>
  83. <td align=center>
  84. 发布时间
  85. </td>
  86. <td align=center>
  87. 管理
  88. </td>
  89. </tr>
  90. <%
  91. do while not rs.eof
  92. %>

  93.   <tr>
  94. <td align=center><input type="checkbox" name="answer" value="<%=rs("id")%>" ID="Checkbox1">
  95. </td>
  96. <td align=left><%If Len(rs("title"))<=30 Then%><%=rs("title")%><%else%>
  97. <%=(Left(rs("title"),30))%>...
  98. <%end if %></td>
  99. <td align=left><%=rs("addtime")%></td>
  100. <td align=center><a href="editnews.asp?id=<%=rs("id")%>">编 辑</a></td>
  101. </tr>
  102. </tbody>
  103. <%
  104. rs.movenext
  105. loop
  106. %>

  107.   <tr>
  108. <td colspan="7" align="center">
  109. <input type="checkbox" name="chkall" value="on" ID="Checkbox2">选中所有的显示新闻   
  110. <input type="button" name="btnDelete" value="删除" style='font-family: 宋体; font-size: 9pt;' ID="Button1">
  111. </td>
  112. </tr>
  113. </table>
  114. </form>
  115. </td>
  116. </tr>
  117. <%end if%>
  118. </table>
  119. <% set rs=nothing
  120. conn.close
  121. set conn=nothing
  122. %>
  123. </body>
  124. </html>
复制代码

delnews.asp文件
  1. <!--#include file="conn.asp"-->
  2. <%
  3. arrdel=Request("id")
  4. 'Response.Write arrdel
  5. sql="delete from news where id in ("&arrdel&")"
  6. 'Response.Write sql
  7. conn.Execute sql
  8. set conn=nothing
  9. response.write"<SCRIPT language=JavaScript>alert('删除成功!');"
  10. response.write"javascript: history.go(-1)</SCRIPT>"
  11. response.end
  12. %>
复制代码



上一篇:asp中用insert into语句向数据库插入记录(添加信息)的方
下一篇:asp 删除数据库记录的代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 06:54

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表