<!--#include file="conn.asp" -->
<%
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from cnarticle"
rs.Open sql,conn,1,1
%>
<%filepath=request.servervariables("path_info")%>
<%
page=request.querystring("page") 'page值为接受值
rs.PageSize = 2 '每页显示记录数
if Not IsEmpty(page) then '如果page已经初始化...
if Not IsNumeric(page) then '判断page值是否为数字
page=1
else
Page = cint(page) '接收page并化为数字型赋给page变量
end if
if Page > rs.PageCount then '如果接收的页数大于总页数
rs.AbsolutePage = rs.PageCount '设置当前显示页等于最后页
elseif Page <= 0 then '如果page小于等于0
rs.AbsolutePage = 1 '设置当前显示页等于第一页
else
rs.AbsolutePage = Page '如果大于零,显示当前页等于接收的页数
end if
else
rs.AbsolutePage=1
end if
Page = rs.AbsolutePage%>
<%
For i = 1 to rs.PageSize '利用for next 循环依次读出当前页的记录
if rs.EOF then
Exit For
end if
response.write("文章标题是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入时间是:"& rs("cn_time"))
response.write("<br>文章内容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Next
%>
<form action="<%=filepath%>" method="get">
<!--首先保证总页数不为1、不为0-->
<%if rs.pagecount<>1 and rs.pagecount<>0 then%>
<!--如果当前页数大于1,无论何时都应显示首页和上一页的连接-->
<%if page>1 then%>
[<a Href="<%=filepath%>?Page=<% = 1%>">首页</a>]
[<a Href="<%=filepath%>?Page=<% = page -1 %>">上一页</a>]
<!--如果当前页数大于1并且小于总页面数时,显示出尾页和下一页的连接-->
<%if page<rs.pagecount then %>
[<a Href="<%=filepath%>?Page=<% = page + 1%>">下一页</a>]
[<a Href="<%=filepath%>?Page=<% = rs.PageCount%>">尾页</a>]
<!--如果当前页数大于1并且仍大于或等于总页面数时,不显示出尾页和下一页的连接-->
<%else%>
[下一页] [尾页]
<%end if%>
<!--否则,当前页数不大于1,则只显示尾页和下一页的连接-->
<%else%>
[首页] [上一页]
[<a Href="<%=filepath%>?Page=<% = page + 1%>">下一页</a>]
[<a Href="<%=filepath%>?Page=<% = rs.PageCount%>">尾页</a>]
<%end if %>
<!--最终,总页数若为1、为0则没有任何连接-->
<%else%>
[首页] [上一页] [下一页] [尾页]
<%end if%>
[页次:<font color=red><b><%=page%></b></font>/<%=rs.PageCount%>]
[共<%=rs.RecordCount%>篇 <font color=red><b><%=rs.PageSize%></b></font>篇/页]
转到<input name="page" size=5 value="<%=page%>">页
<input type="submit" value="Enter">
</form>
<%
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>
长长的分页代码,搞懂是真不容易,平台引用起来还需要修改也是比较麻烦。最后能做成一个函数,下次调用起来就很方便了。
<%
function pagination(pagecount,pagesize,page,resultcount)
Dim query, a, x, temp
action = "http://" & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("SCRIPT_NAME")
query = Split(Request.ServerVariables("QUERY_STRING"), "&")
For Each x In query
a = Split(x, "=")
If StrComp(a(0), "page", vbTextCompare) <> 0 Then
temp = temp & a(0) & "=" & a(1) & "&"
End If
Next
Response.Write("<form method=get onsubmit=""document.location = '" & action & "?" & temp & "Page='+this.page.value;return false;"">")
if page<=1 then
Response.Write ("[首页] [上一页] ")
else
Response.Write("[<a href=" & action & "?" & temp & "Page=1>首页</a>] ")
Response.Write("[<a href=" & action & "?" & temp & "Page=" & (Page-1) & ">上一页</a>] ")
end if
if page>=pagecount then
Response.Write ("[下一页] [尾页]")
else
Response.Write("[<a href=" & action & "?" & temp & "Page=" & (Page+1) & ">下一页</a>] ")
Response.Write("[<a href=" & action & "?" & temp & "Page=" & pagecount & ">尾页</a>]")
end if
Response.Write("[页次:<font color=red>" & page & "</font>/" & pageCount)
Response.Write("] [共" & resultcount & "条 <font color=red>"& pagesize & "</font>条/页]")
Response.Write(" 转到" & "<input name=page size=4 value=" & page & ">" & "页<input type=submit value=go>")
End function
%>
如要引用,则可以:
<%call pagination(rs.PageCount,rs.pagesize,cint(page),rs.RecordCount) %>
前面的代码是
dim page
rs.pagesize=10
if request.querystring("page")=0 or request.querystring("page")="" then
page=1
else
page=request.querystring("page")
rs.absolutepage=trim(request.querystring("page"))
end if
怎么样把最后一个文本框换成列表表单?
Response.Write("转到<select name=page>")
for i = 1 to pagecount
Response.Write("<option value="&i&">")
next
Response.Write("</select>")
一种理论上最快的Web数据库分页方法
上篇我们谈到了关于数据库传统的三种分页方法和他们的利弊,并且提出了一种理论上最佳的分页方法,本篇我们就来详细说说这种最佳的分页方法。
一:构思。
在设计Web数据库时,如果我们要编历每一条纪录,那么只有采取分页模式才可以使Web数据库尽快,尽好的呈现给终端用户,也不会因为8秒原则而使用户失去浏览该页的兴趣。但是即使采取分页的办法,当出现多纪录的数据库时,也难免会使我们的用户感到翻页时速度太慢。就如同我的上篇文章说的那样,几乎上一片文章的三种分页方法都有一些缺陷。那么,我们如何做到能让数据库每次就取我们需要的纪录,这个很好实现,有游标返回多个纪录集就可以实现,但是如果让数据库的一端不会因为要刚好检索一页的纪录而大耗资源就很难了。最后,经过我的不断改写程序与测试,终于编写出了我认为理论上最快的Web数据库分页方法。
二:具体实现的存储过程。
我们结合一个BBS问题来谈谈这种方法。如何让一个BBS每次每页只现实需要的一页纪录呢?而我们需要提供给数据库有那些参数呢?可能会有以下参数。
第一:就是我们需要的当前的页数。
第二:当前定义的每一页的纪录集数目。这样你就可以根据需要在页面程序中修改每一页的纪录数。当然,如果你不考虑程序的可扩展性,你也可以在数据库里直接规定每一页有N条纪录即可。
第三:一个输出参数:就是从数据库里得出当前表中总纪录数目的多少。(注意,他不是一个页的纪录数目)他相当于ADO分页法中的Recordcount。如果你不需要总纪录数目可以不用返回他。
我们来看具体存储过程的代码。。。
CREATE PROCEDURE dbo.PRO_pageview
(
@tint_tableid tinyint=1, --这个是BBS的当前版面Id,你可以不用管他。。
@int_pagenow int=0,
@int_pagesize int=0,
@int_recordcount int=0 output --就是得出BBS某个版面的总贴数。。
)
AS
set nocount on
declare @int_allid int
declare @int_beginid int,@int_endid int
declare @int_pagebegin int, @int_pageend int
select @int_allid=count(*) from tab_discuss where tint_level=0 and tint_tableid=@tint_tableid
select @int_recordcount=@int_allid --得出该版面的总贴数
declare cro_fastread cursor scroll
for select int_id from tab_discuss where tint_level=0 and tint_tableid=@tint_tableid order by int_id desc --这里定义游标操作,但是不用临时纪录集,而且游标也不需要全部遍历所有纪录集。
open cro_fastread --打开游标
select @int_beginid=(@int_pagenow-1)*@int_pagesize+1 得出该页的第一个纪录Id
select @int_endid = @int_beginid+@int_pagesize-1 得出该页的最后一个纪录的Id
fetch absolute @int_beginid from cro_fastread into @int_pagebegin 将他的Id传给一个变量该页开始的Id
if @int_endid>@int_allid --这里要注意,如果某一页不足固定页数的纪录时。如只有一页纪录,而且纪录少于我们定义的数目。或者是最后一页时。。。
fetch last from cro_fastread into @int_pageend --直接将游标绝对定位到最后一条纪录,得出他的id号来。。。
else
fetch absolute @int_endid from cro_fastread into @int_pageend
select int_id,tint_level,tint_children,var_face,var_subject,datalength(txt_content) as int_len,sint_hits,var_url,var_image,var_user,dat_time,tint_tableid,bit_kernul from tab_discuss where tint_tableid=@tint_tableid and int_rootid between @int_pageend and @int_pagebegin order by int_rootid desc,num_order desc --我们就可以利用该页的第一个id和最后一个id得出中间的id来。。。。(注意。我们这个BBS的数性结构用了一种很巧妙的算法,就是用一个orderNum浮点数即可完成排序。。。)
--开始清场。。。
close cro_fastread
deallocate cro_fastread
return
我们再看看Asp页面里的程序操作。。。
pagenow=cint(request("pagenow")) --当前的页面。
if pagenow<=0 then pagenow=1
pagesize=10
set cmd=server.CreateObject("adodb.command")
cmd.ActiveConnection=strconn
cmd.CommandType=4
cmd.CommandText="pro_pageview"
cmd.Parameters.Append cmd.CreateParameter("tint_tableid",adInteger,adParamInput,,tint_tableid)
cmd.Parameters.Append cmd.CreateParameter("int_pagenow",adInteger,adParamInput,,pagenow)
cmd.Parameters.Append cmd.CreateParameter("int_pagesize",adInteger,adParamInput,,pagesize)
cmd.Parameters.Append cmd.CreateParameter("int_recordcount",adInteger,adParamOutput)
set rs=cmd.Execute
if rs.eof then
Response.Write "目前已超过了纪录个数或纪录集为空!"
Response.End
end if
dim arrRs
arrRs=rs.getrows '可以利用getRows将纪录集迅速保存到一个二维的数组中来提高速度。
recordcount=cmd.Parameters("int_recordcount")
'注意,当纪录不足以刚好整除单位页纪录时,我们也要将其定义为一页,如纪录数目为2页多一个纪录,此时我们的页数也要为3页纪录。
if (recordcount mod pagesize)=0 then
pagecount=recordcount/pagesize
else
pagecount=recordcount/pagesize+1
end if
<--分页开始 -->
<!-- #include file="include/tablepage.asp" -->固定的分页函数,其实无非是pagenow+1或pagenow-1,pagenow,pagecount
<!--分页结束-->
<div align="left" class="pblank" >
<%
'---------显示树性结构!-------------
level=0
Response.Write "<ul>"
for i=0 to ubound(arrRs,2)
if arrRs(1,i)>level then
Response.Write "<ul>"
end if
if arrRs(1,i)<level then
for j=arrRs(1,i) to level-1
Response.Write "</ul>"
next
end if
int_size=arrRs(5,i)
if int_size=0 then
str_size=" <无内容>"
else
str_size=""
end if
Response.Write "<li><img src=face/"&arrRs(3,i)&"><a href=showTitle.asp?int_id="&arrRs(0,i)&"&tint_tableid="&tint_tableid&" class=ptitle target=BoardAnnounce>"&server.HTMLEncode(arrRs(4,i))&"</a> "&str_size
if arrRs(7,i)<>"" then Response.Write " <连接> "
if arrRs(8,i)<>"" then Response.Write " <图像> "
Response.Write " -【"&arrRs(9,i)&"】 <font color=444444>"&arrRs(10,i)&"</font> [<font color=920092>ID:"&arrRs(0,i)&" 点击:"&arrRs(6,i)&"次</font>] <font color=444444>("&int_size&"字节)</font> <font color=ff0000>("&arrRs(2,i)&")</font></li>"
level=arrRs(1,i)
next
Response.Write "</ul>"
'---------显示树性结构完毕!-------------
%>
</div>
<div align="left">
<!--分页开始 -->
<!-- #include file="include/tablepage.asp" -->
<!--分页结束-->
</div>
<!-- 清场与除错 -->
<%
rs.close
set rs=nothing
set cmd=nothing
if err.number<>0 then Response.Redirect "bug.asp"
%>
三:特点
我们来看看他和传统的三种方法的区别与特点
第一:每次只传回一页纪录,而且只形成一个纪录集,而且客户端可以采用速度最快的火线游标来完成页面输出。而不必像传统的游标法用rs.nextrecordset纪录来输出纪录。
第二:数据库没有用到临时表,这样比转储纪录集的速度大大提高。
第三:采用一个滚动游标,而且游标只经过二个操作就完成定位。速度也大大提高。
当我采用了这种分页方法时,已经可以明显的感觉出分页速度的提高了。当然,在处理树型结构,数据库运算时,我采用了许多方法尽可能的提高速度,如:采用二分区间法来编历树型结构,全部采用存储过程来实现一切sql操作,采用触发器和数据库游标来完成数据库算法,这样就尽量避免过多的网络传输。任何操作只和数据库进行一次参数传递就可以完成。
另一种方法就是
pagesize:每页条数
第n页
select top "&pagesize&" * from 表名 where id not in (select top "&pagesize*(n-1)&" id from 表名 order by id)order by id
id 是primary key
这篇博客探讨了ASP分页的方法,包括传统方法的优缺点和一种理论上最快的分页实现。通过创建存储过程,结合游标和特定的参数(当前页数、每页记录数、总记录数),实现了每次仅检索所需页面记录的功能,优化了数据库性能。此外,还提供了一个可调用的分页函数示例,以提高页面加载速度。
945

被折叠的 条评论
为什么被折叠?



