Access2000資料庫80萬記錄通用快速ASP分頁類

主要思路: 用一筆語句統計(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 sintPageMax - sintPageNow < 1 then
sstrPageInfo = sstrPageInfo & " 下一頁 末頁 "
Else
sstrPageInfo = sstrPageInfo & " 下一頁 "
sstrPageInfo = sstrPageInfo & " 末頁 "
End If

sstrPageInfo = sstrPageInfo & " 頁次:" & sintPageNow & " / " & sintPageMax & " "
sstrPageInfo = sstrPageInfo & " 共 " & sintRecordCount & " 條記錄 " & sintPageSize & " 條/頁 "
End Sub

Rem ## 長整數轉換
Private function toNum(s, Default)
s = s & ""
If s <> "" And IsNumeric(s) Then
toNum = CLng(s)
Else
toNum = Default
End If
End function

Rem ## 類初始化
Public Sub InitClass()
sbooInitState = True
If Not(IsObject(sobjConn)) Then sbooInitState = False
Call InitRecordCount()
Call InitPageInfo()
End Sub
End Class
Dim strLocalUrl
strLocalUrl = request.ServerVariables("SCRIPT_NAME")

Dim intPageNow
intPageNow = request.QueryString("page")

Dim intPageSize, strPageInfo
intPageSize = 30

Dim arrRecordInfo, i
Dim Conn
f__OpenConn
Dim clsRecordInfo
Set clsRecordInfo = New Cls_PageView

clsRecordInfo.strTableName = "[myTable]"
clsRecordInfo.strPageUrl = strLocalUrl
clsRecordInfo.strFieldsList = "[ID], [Title], [LastTime]"
clsRecordInfo.strCondiction = "[ID] < 10000"
clsRecordInfo.strOrderList = "[ID] ASC"
clsRecordInfo.strPrimaryKey = "[ID]"
clsRecordInfo.intPageSize = 20
clsRecordInfo.intPageNow = intPageNow

clsRecordInfo.strCookiesName = "RecordCount"
clsRecordInfo.strPageVar = "page"

clsRecordInfo.intRefresh = 0
clsRecordInfo.objConn = Conn
clsRecordInfo.InitClass

arrRecordInfo = clsRecordInfo.arrRecordInfo
strPageInfo = clsRecordInfo.strPageInfo
Set clsRecordInfo = nothing
f__CloseConn
%>



分頁測試








<%= strPageInfo%>








<%
If IsArray(arrRecordInfo) Then
For i = 0 to UBound(arrRecordInfo, 2)
%>





<%
Next
End If
%>
ID描述日期
<%= arrRecordInfo(0, i)%> <%= arrRecordInfo(1, i)%> <%= arrRecordInfo(2, i)%>






<%= strPageInfo%>





<%= getTimeOver(1)%>





來源:http://www.d9soft.com/article/120/Article5900_1.htm
本文章來自於神魂顛倒論壇 http://bbs.flash2u.com.tw
原文網址:http://bbs.flash2u.com.tw/dispbbs_58_53765__next.html


上一篇: 海量信息ASP分页类增强版
下一篇: asp批量修改成功和失败的代码比较
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1242
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 300 字 | UBB代码 开启 | [img]标签 关闭