Access 2000 數(shù)據(jù)庫 80 萬記錄通用快速分頁類的主要思路: 用一條語句統(tǒng)計(count)出記錄數(shù)(而不在查詢時獲得 recordcount 屬性), 緩存在 cookies 中, 跳轉(zhuǎn)時就不用再次統(tǒng)計。使用 ado 的 absolutepage 屬性進行頁面跳轉(zhuǎn)即可, 為方便調(diào)用而寫成類, 代碼主要地方已有說明
硬件環(huán)境: amd athlon xp 2600+, 256 ddr
軟件環(huán)境: ms windows 2000 advanced server + iis 5.0 + access 2000 + ie 6.0
測試結(jié)果: 初次運行在 250(首頁) - 400(末頁)毫秒, (記錄數(shù)緩存后)在頁面間跳轉(zhuǎn)穩(wěn)定在 47 毫秒以下.第1頁跳到最后一頁不多于 350 毫秒
適用范圍: 用于普通分頁. 不適用于有較復(fù)雜的查詢時: 如條件為"[title] like %最愛%", 查詢的時間大大增加, 就算 title 字段作了索引也沒用. :(
<%@language = "vbscript" codepage="936"%>
<%option explicit%>
<%
dim intdatestart
intdatestart = timer()
rem ## 打開數(shù)據(jù)庫連接
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 ## 關(guān)閉數(shù)據(jù)庫連接
rem #################################################################
function f__closeconn()
if isobject(conn) then
conn.close
end if
set conn = nothing
end function
rem #################################################################
rem 獲得執(zhí)行時間
rem #################################################################
function gettimeover(iflag)
dim ttimeover
if iflag = 1 then
ttimeover = formatnumber(timer() - intdatestart, 6, true)
gettimeover = " 執(zhí)行時間: " & ttimeover & " 秒"
else
ttimeover = formatnumber((timer() - intdatestart) * 1000, 3, true)
gettimeover = " 執(zhí)行時間: " & 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 ## 保存記錄數(shù)的 cookies 變量
public property let strcookiesname(value)
sstrcookiesname = value
end property
rem ## 轉(zhuǎn)向地址
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 ## 用于統(tǒng)計記錄數(shù)的字段
public property let strprimarykey(value)
sstrprimarykey = value
end property
rem ## 每頁顯示的記錄條數(shù)
public property let intpagesize(value)
sintpagesize = tonum(value, 20)
end property
rem ## 數(shù)據(jù)庫連接對象
public property let objconn(value)
set sobjconn = value
end property
rem ## 當前頁
public property let intpagenow(value)
sintpagenow = tonum(value, 1)
end property
rem ## 頁面參數(shù)
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 ## 取得記錄集, 二維數(shù)組或字串, 在進行循環(huán)輸出時必須用 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 ## 初始化記錄數(shù)
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