電子工業出版社 SHING HOUSE DF ELECTRONCS IND 【例15.6】编程实现下图15.10及15.11的结果:在浏览器上显示Demo数据库 users 表中的所有记录,删除用户确定的记录。(基础模块2.3”删除记录”) ③历小8·⑥0你 撙记录 请选择要除的记录 电子邮箱 yty barr 成 典开时“的档10J国管度真」是|)动块量,回b9国 图15.10选择要删除的记录 ③历+日即0 刪除记最 您所选择的记录已删除,数据厍中剩余的记录为: 图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】的模块中这两个程序段相同。只要检查修改相应的参数,就可直接引 用这两个程序段
【例 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】的模块中这两个程序段相同。只要检查修改相应的参数,就可直接引 用这两个程序段
電子工業出版社 JEUISHING HOUSE DF ELECTRONCS INDSTR <Sele_ dele record.htm>源程序 < Option Explicit %><% 建立一个到数据源的连接 strDSN="Provider=MSDASQL: DRIVER=SQL rver): SERVER=127. 0. 0. 1: DATABASE=Demo: UID=chen; PWD=123 Set conn Demo Server CreateObject("ADODB Connection") 建立记录集,存放查询结果 Dim strSqlSelectUsers Set rsUsers Server CreateObject("ADODB. Recordset") strSqlselectUsers= SELECT FROM users rsUsers. Open strSqISelectUsers, connDemo > 一结果输出,以供选择 <html> metahttp-equiv=content-typecontent=text/html:charset=gb2312> < title>基础模块“删除记录”之“选择记录”</ title〉 <body bgcolor=#COCco"> font face="华文行楷”size="6">选择记录</ font></hl <h3>请选择要删除的记录:</h3 If Not rsusers. Eof then Form me thod="post action=delete record. asp> Ktable border="l" cellpadding=8 cellspacing=0 width=342> < th width="70″>姓名《/th < th width="121″>电话√/th> < th width="143″>电子邮箱</th> <th</th>
<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>
電子工業出版社 JEUISHING HOUSE DF ELECTRONCS INDSTR Dim idCollection Do While not rsUsers. eof 将用户所选中的所有记录的id号存放在 coLlection变量中,用","作为分隔符 idCollection=idCollection rsUsers(id)& <td width="70><% =rsUsers("username")%></td> <td width=121"><% =rsUsers(phone)%></td> <td width="143"><% =rsUsers(email")%>/td> td> !一-在每条记录后添加一个检查框,供用户选定记录 Input name="chk No<% =rsUsers (id")%> type="checkbox"> rsUsers. MoveNex End If rsUsers. Close Set rsUsers=Nothing 将 coLlection贮存在一个隐藏对象中,以传递给下一个页面 Response Write(<input type=hidden name='hidIdCollection' value="& idCollection &">") < input type=" submit" value="删除记录" </form> </html> delete record.asp程序流程如下图15.13所示:
<% 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 所示:
電子工業出版社 JEUISHING HOUSE DF ELECTRONCS INDSTR 建立一个到数据源的连结 删除记录 建立记录集,存放查询结 匚输出结果 图15.13 delete record.asp程序流程图 从程序流程图中可以看出,程序段“建立一个到数据源的连结”“建立记录集,存放査 询结果”及“输出结果”与【例15.4】模块中这三个程序段相同.只要检査修改相应的参数 就可直接引用这三个程序段。 < delete record.asp>源程序: Option Explicit % 建立一个到数据源的连接 Dim strDsN Dim conn de strDSN="Provider=MSDASQL: DRIVER=SQL erverl: SERVER=127. 0.0. 1: DATABASE=Demo: UID=chen: PWD=123 obJect ("ADOD connDemo. Open strDSN 删除记录 Dim idCollection, strEngth, totalOfIds, 1, id, strSq lDeleteUser idCollection=request form("hidIdCollection") strEngth=Len (idCollection) idCollection=Left(idCollection, strLength-1) idCollection=Split(idCollection, ", " totalOfIds=UBound (idCollection, 1) For i=0 to totaloflds id=Request. Form("chkNo"& idCollection(i)) If Not strsq dEletes DELETE FROM users WhEre id="& Clng(idCollection(i)) connDemo, Execute strSqlDeleteUser End if
图 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
電子工業出版社 JEUISHING HOUSE DF ELECTRONCS INDSTR Next 建立记录集,存放查询结果 sers Dim strSql SelectUsers Set rsUsers= Server CreateObject("ADODB. Recordset) strSqlselectUsers=" SELECT FROM users rsUsers. Open strSqlselectUsers, connDemo !一结果输出-一 <html> Head> metahttp-equiv=content-typecontent=text/html:charset=gb2312> < title>基础模块“删除记录”之“删除记录”/ title </head <body bgcolor=#COCOC0"> font face="华文行楷”size="6">删除记录<font>/hl h3》您所选择的记录已删除。数据库中剩余的记录为:</h3 If Not rsUsers. Eof Then <table border="l" cellpadding=8 cellspacing=0 width=342"> < th width="70″>姓名</th < th width="121″>电话/th < th width="143″>电子邮箱√/th> Do While not rsusers. eof <td width=70"><% =rsUsers("username )%></td> <td width="121"><% =rsUsers(phone")%></td> <td width="143"><% =rsUsers (email")%></td>
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>