【例15.6】 编程实现下图15.10及15.11的结果:在浏览器上显示Demo数据库users表中的所有记录,删除用户确定的记录。(基础模块2.3”删除记录”)

图15.10 选择要删除的记录

图15.11 删除结果显示
“删除记录”模块由两段程序代码(sele_dele_record.htm与delete_record.asp)组成。Sele_dele_record.htm是选择要删除的记录,delete_record.asp是从数据库中删除选定的记录。
Sele_dele_record.htm程序流程如下图15.12所示:

图 15.12 Sele_dele_record.htm程序流程图从程序流程图中可以看出,程序段“建立一个到数据源的连结”“建立记录集,存放查询结果”与【例15.4】的模块中这两个程序段相同。只要检查修改相应的参数,就可直接引用这两个程序段。
<Sele_dele_record.htm>源程序:
<% Option Explicit %><%
'建立一个到数据源的连接
Dim strDSN
Dim connDemo
strDSN="Provider=MSDASQL;DRIVER={SQL Server};SERVER=127.0.0.1;DATABASE=Demo;UID=chen;PWD=123"
Set connDemo = Server.CreateObject("ADODB.Connection")
connDemo.Open strDSN
'建立记录集,存放查询结果
Dim rsUsers
Dim strSqlSelectUsers
Set rsUsers = Server.CreateObject("ADODB.Recordset")
strSqlSelectUsers="SELECT * FROM users"
rsUsers.Open strSqlSelectUsers,connDemo
%>

<!--结果输出,以供选择-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>基础模块“删除记录”之“选择记录”</title>
</head>
<body bgcolor="#C0C0C0">
<p>
<font face="华文行楷" size="6">选择记录</font></h1>
</p>
<h3>请选择要删除的记录:</h3>
<%
If Not rsUsers.Eof Then
%>
<form method="post" action="delete_record.asp">
<table border="1" cellpadding="8" cellspacing="0" width="342">
<tr>
<th width="70" >姓名</th>
<th width="121" >电话</th>
<th width="143" >电子邮箱</th>
<th></th>
</tr>

<%
Dim idCollection
Do While Not rsUsers.Eof

'将用户所选中的所有记录的id号存放在idCollection变量中,用","作为分隔符
idCollection=idCollection & rsUsers("id") & ","
%>
<tr>
<td width="70"><% =rsUsers("username") %></td>
<td width="121"><% =rsUsers("phone") %></td>
<td width="143"><% =rsUsers("email") %></td>
<td>
<!--在每条记录后添加一个检查框,供用户选定记录-->
<input name="chkNo<% =rsUsers("id") %>" type="checkbox">
</td>
</tr>
<%
rsUsers.MoveNext
Loop
End If

rsUsers.Close
Set rsUsers=Nothing

'将idCollection贮存在一个隐藏对象中,以传递给下一个页面
Response.Write("<input type='hidden' name='hidIdCollection' value=" & idCollection & ">")
%>

</table>
<input type="submit" value="删除记录">
</form>
</body>
</html>
delete_record.asp程序流程如下图15.13所示:
  
图15.13 delete_record.asp程序流程图
从程序流程图中可以看出,程序段“建立一个到数据源的连结”“建立记录集,存放查询结果”及“输出结果”与【例15.4】模块中这三个程序段相同.只要检查修改相应的参数,就可直接引用这三个程序段。
<delete_record.asp>源程序:
<% Option Explicit %>
<%
'建立一个到数据源的连接
Dim strDSN
Dim connDemo
strDSN="Provider=MSDASQL;DRIVER={SQL Server};SERVER=127.0.0.1;DATABASE=Demo;UID=chen;PWD=123"
Set connDemo = Server.CreateObject("ADODB.Connection")
connDemo.Open strDSN

'删除记录
Dim idCollection,strLength,totalOfIds,i,id,strSqlDeleteUser

idCollection=request.form("hidIdCollection")
strLength=Len(idCollection)
idCollection=Left(idCollection,strLength-1)
idCollection=Split(idCollection,",")
totalOfIds=UBound(idCollection,1)

For i=0 to totalOfIds
id=Request.Form("chkNo" & idCollection(i))
If Not IsEmpty(id) Then
strSqlDeleteUser="DELETE FROM users WHERE id=" & Clng(idCollection(i))
connDemo.Execute strSqlDeleteUser
End If
Next

'建立记录集,存放查询结果
Dim rsUsers
Dim strSqlSelectUsers
Set rsUsers = Server.CreateObject("ADODB.Recordset")
strSqlSelectUsers="SELECT * FROM users"
rsUsers.Open strSqlSelectUsers,connDemo
%>
<!--结果输出-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>基础模块“删除记录”之“删除记录”</title>
</head>
<body bgcolor="#C0C0C0">
<p>
<font face="华文行楷" size="6">删除记录</font></h1>
</p>
<h3>您所选择的记录已删除。数据库中剩余的记录为:</h3>

<%
If Not rsUsers.Eof Then
%>
<table border="1" cellpadding="8" cellspacing="0" width="342">
<tr>
<th width="70" >姓名</th>
<th width="121" >电话</th>
<th width="143" >电子邮箱</th>
</tr

<%
Do While Not rsUsers.Eof
%>
<tr>
<td width="70"><% =rsUsers("username") %></td>
<td width="121"><% =rsUsers("phone") %></td>
<td width="143"><% =rsUsers("email") %></td>
</tr>
<%
rsUsers.MoveNext
Loop
End If
%>
</table>
</body>
</html>