WebComputing━ADO总结报告7

网络整理 - 08-26
实例一:分页显示及导航:
为什么我要再提分页的这个问题呢?因为这是一个最基本的问题,虽然有很多关于分页的文章,但我觉得他们的方法偏于复杂。其实RecordSet的AbsolutePage就可以轻松的实现分页,当你指定了PageSize属性后,对AbsolutePage指定值就可以翻转到指定的页面。但是如果你想使用AbsolutePage的话,你必须在打开RecordSet对象之前将它的CursorLocation值设为adUseClient,这个属性是继承Connection对象的一个相同属性的。你也可以在打开Connection对象之前来设定它。下面是源代码,为了方便,我将导航栏独立成了一个子程序方便大家使用。
<%
sub navigator(PageNo,Target)
Response.Write "<table border=0>"
Response.Write "<tr>"
Response.Write "<td>"
if PageNo>1 then
Response.write "<a href="&chr(34)&Target&"?Page=1"&chr(34)&">┃第一页</a>"
else
Response.Write "┃第一页"
end if
Response.Write "</td>"
Response.Write "<td>"
if PageNo<rs.PageCount then
Response.write"<a href="&chr(34)&Target&"?Page="&PageNo+1&chr(34)&">┃下一页</a>"
else
Response.Write "┃下一页"
end if
Response.Write "</td>"
Response.Write "<td>"
if PageNo>1 then
Response.write "<a href="&chr(34)&Target&"?Page="&PageNo-1&chr(34)&">┃前一页</a>"
else
Response.Write "┃前一页"
end if
Response.Write "</td>"
Response.Write "<td>"
if PageNo<rs.PageCount then
Response.write "<a href="&chr(34)&Target&"?Page="&rs.PageCount&chr(34)&">┃最后一页</a>"
else
Response.Write "┃最后一页"
end if
Response.Write "</td>"
Response.Write "<td>"
Response.Write "┃页次:"&PageNo&"/"&rs.PageCount&"页┃"&rs.PageSize&"条记录/页┃"
Response.Write "</td>"
Response.Write "<td valign="middle">"
Response.Write "<form action="&chr(34)&Target&chr(34)&" method="&chr(34)&"POST"&chr(34)&">"
Response.Write "<input type="text"size=3 maxlength=4 name="Page">"
Response.Write " <input type="submit"value="转到">"
Response.Write "</form>"
Response.Write "</td>"
Response.Write "</tr>"
Response.Write "</table>"
end sub
%>

<%
const adCmdText=&H0001
const adVarChar=200
const adInteger=3
const adParamInput=&H0001
const adCmdTable=&H0002
const adUseClient=3
const adDate=7
const adLongVarChar=201
set conn=Server.CreateObject("ADODB.Connection")
conn.ConnectionString="Driver={Microsoft Access Driver (*.mdb)};DBQ="& _
Server.Mappath("/source_asp")&"/process/process.mdb;"
conn.Open
%>

<%
const MaxPageSize=5
%>
<html>
<head>
<title> See Book </title>
</head>
<body>
<%
dim i,j,PageNo
set rs=Server.CreateObject("ADODB.RecordSet")
rs.ActiveConnection=conn
rs.CursorLocation=adUseClient
rs.Open "Select * From books",,,adCmdText
if rs.BOF then
Response.Write "欢迎使用图书,资料管理程序!"
else
rs.PageSize=MaxPageSize
if isempty(Request.QueryString("Page")) then
PageNo=1
elseif cInt(Request.QueryString("Page"))<1 then
PageNo=1
elseif cInt(Request.QueryString("Page"))>rs.PageCount then
PageNo=rs.PageCount
else
PageNo=cInt(Request.QueryString("Page"))
end if
if Request.ServerVariables("Request_Method")="POST" and not Isempty(Request.Form("Page")) then
PageNo=cInt(Request.Form("Page"))
end if
rs.AbsolutePage=PageNo
Response.Write "<table border="0" width="100%">"
Response.Write "<tr><td colspan="&rs.fields.count&">"
Target="books.asp"
call navigator(PageNo,Target) "调用导航栏
Response.Write "</td></tr>"
Response.Write "<tr>"
for i=0 to rs.fields.count-1
Response.Write "<td>"&rs.fields.item(i).name&"</td>"
next
Response.Write "</tr>"
j=0
while (not rs.EOF) and j<rs.PageSize
Response.Write "<tr>"
for i=0 to rs.fields.count-1
if i=1 then
Response.Write "<td>"&"<a href="&chr(34)&"status.asp?BookName="& _
rs.fields.item(i).value&chr(34)&">"&rs.fields.item(i).value&"</a></td>"
"这里这样写是为了级联式查询而做的。
else
Response.Write "<td>"&rs.fields.item(i).value&"</td>"
end if
next
Response.Write "</tr>"
rs.MoveNext
j=j+1
wend
Response.Write "</table>"
end if
%>