木子屋 Dnawo's BLOG

关于分页的一点思路

👤 dnawo 📅 2007-02-15 👁 6044 👍 0 💬 0 🔄 来源:本站原创
前言

很多ASP方面的书籍在讲述分页的时候都是利用RecordSet对象的pagesize、absolutepage属性进行,这种方法很简单,很多ASP程序员在做分页程序时用的也就是这个方法。这方法在小型系统中是可取的,但在大系统中记录达到百万级、千万级的时候缺点就变得非常突出了:非常耗系统资源,原因非常简单:这种方法在读取数据时,先不管你要取哪些记录,都是一次性先把所以记录读取出来保存在RecordSet对象中,再进行分页,如果多人同时进行读取的话,严重时将导致当机。

其实在我刚接触分页这个概念时就想这不是一件容易的事么?比如每页要显示10条记录,显示第1页时只需在查询条件中加入“id>=1 and id<11”,显示第2则为“id>=11 and id<21”,很简单嘛!后来才发觉没有想像中的容易,因为在删除记录后id就变得不连续了,用这种方法就行不通了。但我们可以转个思路:既然不能用页数来判断出每页第一条记录的id和最后一条记录的id,但如果我们事先就知道每页第一条记录及最后一记录id的话,不就还能用上边的方法了吗?呵呵。根据每页显示的记录数得出每页第一条记录和最后一条记录的id不难,问题是这些id应以什么样的形式保存?保存在哪边?你可以将这些id保存在一个文本文件中,也可以将它们保存在Application对象中以供调用,下边我们要讲的就是利用第二种方法。

实现原理

将每页第一条记录及最后一条记录的id以逗号分隔组成一个字符串保存到Application对象中,当然,这条语句必须放在Global.asa文件的application_onstart过程内;然后自定义一个过程,其作用是更新Application对象的内容及重写Global.asa文件,在每次添加或删除记录时就得调用下这个过程。

应用举例

1.新建一个Global.asa文件,内容可以为空。

2.定义一个过程loadpages,其功能是更新Application对象的内容及重写Global.asa文件。
'函数名称:loadpages
'功能说明:搜索指定表,每页显示Pagesize条记录,将每页第一条记录及最后一条记录的id以逗号分隔组成一个字符串保存到Application对象中并更新Global.asa文件
'参数说明:sTable:搜索表名
'其他说明:1.必须先新有Connection对象实例conn
'          2.数据库中必须有id字段,类型为自动编号
'          3.注意Global.asa文件路径
Sub loadpages(sTable)
	'以下更新Application对象中的值
	Dim Pagesize,LoadSql,LoadRs
	Pagesize = 20
	Application.Lock
	Application("pages") = ""
	LoadSql = "Select * from " & sTable & " order by id desc"
	Set LoadRs = Server.CreateObject("ADODB.RecordSet")
	LoadRs.open LoadSql,conn,1,2
	If not LoadRs.eof then
		If LoadRs.RecordCount < Pagesize then
			Application("pages") = Application("pages") & LoadRs("id") & ","
			LoadRs.movelast
			Application("pages") = Application("pages") & LoadRs("id")
		Else
			Dim i
			Do while not LoadRs.eof
				Application("pages") = Application("pages") & LoadRs("id") & ","
				LoadRs.move(Pagesize)
			Loop
			If LoadRs.RecordCount mod Pagesize <> 1 then
				LoadRs.movelast
				Application("pages") = Application("pages") & LoadRs("id") & ","
			End if
			Application("pages") = Left(Application("pages"),len(Application("pages"))-1)
		End if
	Else
		Application("pages") = "nopages"
	End if
	LoadRs.close
	Set LoadRs = nothing
	Application.Unlock
	
	'以下更新Global.asa文件
	Dim fso,path
	path = Server.MapPath("Global.asa")
	Set fso = Server.CreateObject("Scripting.FileSystemObject")
	If fso.FileExists(path) then
		Dim TextFile,content
		Set TextFile = fso.OpenTextFile(path,2,true)
		content = "<Script language=""VBScript"" runat=""server"">" & vbcrlf
		content = content & "Sub application_onstart" & vbcrlf
		content = content & "Application.lock" & vbcrlf
		content = content & "Application(""pages"")=""" & Application("pages") & """" & vbcrlf
		content = content & "Application.Unlock" & vbcrlf
		content = content & "End Sub" & vbcrlf
		content = content & "</Script>" & vbcrlf
		TextFile.write content
		Set TextFile = nothing
	End if
	Set fso = nothing
End Sub


3.下边过程用于显示分页列表
'函数名称:pageslist
'功能说明:显示分页列表
'参数说明:newview:浏览信息面文件名
'其他说明:1.page_no为当前页面参数
Sub pageslist(newview)
	Dim arr,pages,page_no
	arr = Split(Application("pages"),",")
	pages = Ubound(arr)
	page_no = request.QueryString("page_no")
	page_no = Cint(page_no)
	If page_no <1 then page_no = 1
	If page_no >pages then page_no = pages
	'首页
	Response.write "<a href=""" & newview & "?page_no=1"">首页</a>"
	'上一页
	If page_no = 1 then
		Response.write "上一页"
	Else
		Response.write "<a href=""" & newview & "?page_no=" & page_no-1 & """>上一页</a>"
	End if
	'下一页
	If page_no = pages then
		Response.write "下一页"
	Else
		Response.write "<a href=""" & newview & "?page_no=" & page_no+1 & """>上一页</a>"
	End if
	'尾页
	Response.write "<a href=""" & newview & "?page_no=" & pages & """>尾页</a>"
End Sub


4.应用页面:
<!--#include file="Conn.asp" -->
<%
Dim arr,pages,page_no
arr = Split(Application("pages"),",")
pages = Ubound(arr)
page_no = request.QueryString("page_no")
If isnull(page_no) then page_no = 1
page_no = Cint(page_no)
If page_no <1 then page_no = 1
If page_no >pages then page_no = pages
Dim sqlstr,rs
If page_no = pages then
	sqlstr = "select * from news2 where id<=" & arr(page_no-1) & " and id>=" & arr(page_no) & " order by id desc"
Else
	sqlstr = "select * from news2 where id<=" & arr(page_no-1) & " and id>" & arr(page_no) & " order by id desc"
End if
Set rs = Server.CreateObject("ADODB.RecordSet")
rs.open sqlstr,conn,1,2
do while not rs.eof
	Response.write rs("id") & ","
	rs.movenext
Loop
Call pageslist("newsview.asp")
rs.close
Set rs = nothing
conn.close
Set conn = nothing
%>


5.注意:添加或删除记录后必须调用一次loadpages过程!

评论(0)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 ASP分页方法总述 下一篇 → 0+1=1 && 1+1>2