Access 2000 数据库 80 万记录通用快速分页类的主要思路: 用一条语句统计(count)出记录数(而不在查询时获得 recordcount 属性), 缓存在 cookies 中, 跳转时就不用再次统计。使用 ado 的 absolutepage 属性进行页面跳转即可, 为方便调用而写成类, 代码主要地方已有说明
硬件环境: amd athlon xp 2600+, 256 ddr
软件环境: ms windows 2000 advanced server + iis 5.0 + access 2000 + ie 6.0
测试结果: 初次运行在 250(首页) - 400(末页)毫秒, (记录数缓存后)在页面间跳转稳定在 47 毫秒以下.第1页跳到最后一页不多于 350 毫秒
适用范围: 用于普通分页. 不适用于有较复杂的查询时: 如条件为"[title] like %最爱%", 查询的时间大大增加, 就算 title 字段作了索引也没用. :(
<%@language = "vbscript" codepage="936"%>
<%option explicit%>
<%
dim intdatestart
intdatestart = timer()
rem ## 打开数据库连接
rem #################################################################
function f__openconn()
dim strdbpath
dim connstr
strdbpath = "../db/test.mdb"
connstr = "provider=microsoft.jet.oledb.4.0;data source="
connstr = connstr & server.mappath(strdbpath)
set conn = server.createobject("adodb.connection")
conn.open connstr
end function
rem #################################################################
rem ## 关闭数据库连接
rem #################################################################
function f__closeconn()
if isobject(conn) then
conn.close
end if
set conn = nothing
end function
rem #################################################################
rem 获得执行时间
rem #################################################################
function gettimeover(iflag)
dim ttimeover
if iflag = 1 then
ttimeover = formatnumber(timer() - intdatestart, 6, true)
gettimeover = " 执行时间: " & ttimeover & " 秒"
else
ttimeover = formatnumber((timer() - intdatestart) * 1000, 3, true)
gettimeover = " 执行时间: " & ttimeover & " 毫秒"
end if
end function
rem #################################################################
class cls_pageview
private sbooinitstate
private sstrcookiesname
private sstrpageurl
private sstrpagevar
private sstrtablename
private sstrfieldslist
private sstrcondiction
private sstrorderlist
private sstrprimarykey
private sintrefresh
private sintrecordcount
private sintpagesize
private sintpagenow
private sintpagemax
private sobjconn
private sstrpageinfo
private sub class_initialize
call clearvars()
end sub
private sub class_terminate()
set sobjconn = nothing
end sub
public sub clearvars()
sbooinitstate = false
sstrcookiesname = ""
sstrpageurl = ""
sstrpagevar = "page"
sstrtablename = ""
sstrfieldslist = ""
sstrcondiction = ""
sstrorderlist = ""
sstrprimarykey = ""
sintrefresh = 0
sintrecordcount = 0
sintpagesize = 0
sintpagenow = 0
sintpagemax = 0
end sub
rem ## 保存记录数的 cookies 变量
public property let strcookiesname(value)
sstrcookiesname = value
end property
rem ## 转向地址
public property let strpageurl(value)
sstrpageurl = value
end property
rem ## 表名
public property let strtablename(value)
sstrtablename = value
end property
rem ## 字段列表
public property let strfieldslist(value)
sstrfieldslist = value
end property
rem ## 查询条件
public property let strcondiction(value)
if value <> "" then
sstrcondiction = " where " & value
else
sstrcondiction = ""
end if
end property
rem ## 排序字段, 如: [id] asc, [createdatetime] desc
public property let strorderlist(value)
if value <> "" then
sstrorderlist = " order by " & value
else
sstrorderlist = ""
end if
end property
rem ## 用于统计记录数的字段
public property let strprimarykey(value)
sstrprimarykey = value
end property
rem ## 每页显示的记录条数
public property let intpagesize(value)
sintpagesize = tonum(value, 20)
end property
rem ## 数据库连接对象
public property let objconn(value)
set sobjconn = value
end property
rem ## 当前页
public property let intpagenow(value)
sintpagenow = tonum(value, 1)
end property
rem ## 页面参数
public property let strpagevar(value)
sstrpagevar = value
end property
rem ## 是否刷新. 1 为刷新, 其他值则不刷新
public property let intrefresh(value)
sintrefresh = tonum(value, 0)
end property
rem ## 获得当前页
public property get intpagenow()
intpagenow = singpagenow
end property
rem ## 分页信息
public property get strpageinfo()
strpageinfo = sstrpageinfo
end property
rem ## 取得记录集, 二维数组或字串, 在进行循环输出时必须用 isarray() 判断
public property get arrrecordinfo()
if not sbooinitstate then
exit property
end if
dim rs, sql
sql = "select " & sstrfieldslist & _
" from " & sstrtablename & _
sstrcondiction & _
sstrorderlist
set rs = server.createobject("adodb.recordset")
rs.open sql, sobjconn, 1, 1
if not(rs.eof or rs.bof) then
rs.pagesize = sintpagesize
rs.absolutepage = sintpagenow
if not(rs.eof or rs.bof) then
arrrecordinfo = rs.getrows(sintpagesize)
else
arrrecordinfo = ""
end if
else
arrrecordinfo = ""
end if
rs.close
set rs = nothing
end property
rem ## 初始化记录数
private sub initrecordcount()
sintrecordcount = 0
if not(sbooinitstate) then exit sub
dim sinttmp
sinttmp = tonum(request.cookies("_xp_" & sstrcookiesname), -1)
if ((sinttmp < 0) or (sintrefresh = 1))then
dim sql, rs
sql = "select count(" & sstrprimarykey & ")" & _
" from " & sstrtablename & _
sstrcondiction
set rs = sobjconn.execute(sql)
if rs.eof or rs.bof then
sinttmp = 0
else
sinttmp = rs(0)
end if
sintrecordcount = sinttmp
response.cookies("_xp_" & sstrcookiesname) = sinttmp
else
sintrecordcount = sinttmp
end if
end sub
rem ## 初始化分页信息
private sub initpageinfo()
sstrpageinfo = ""
if not(sbooinitstate) then exit sub
dim surl
surl = sstrpageurl
if instr(1, surl, "?", 1) > 0 then
surl = surl & "&" & sstrpagevar & "="
else
surl = surl & "?" & sstrpagevar & "="
end if
if sintpagenow <= 0 then sintpagenow = 1
if sintrecordcount mod sintpagesize = 0 then
sintpagemax = sintrecordcount \ sintpagesize
else
sintpagemax = sintrecordcount \ sintpagesize + 1
end if
if sintpagenow > sintpagemax then sintpagenow = sintpagemax
if sintpagenow <= 1 then
sstrpageinfo = "首页 上一页"
else
sstrpageinfo = sstrpageinfo & " 首页"
sstrpageinfo = sstrpageinfo & " 上一页"
end if
if sintpage