欢迎光临
我们一直在努力

Access 2000 数据库 80 万记录通用快速分页类-ASP教程,数据库相关

建站超值云服务器,限时71元/月

作者:萧月痕(xiaoyuehen)

地址:http://blog.csdn.net/xiaoyuehen/archive/2005/01/17/257202.aspx

转贴请包含相关信息, 谢谢.

主要思路: 用一条语句统计(count)出记录数(而不在查询时获得 recordcount 属性), 缓存在 cookies 中, 跳转时就不用再次统计, 分页跳转链接也由此来, 使用 ado 的 absolutepage 属性设置当前页面, 返回intpagesize 条记录. 为方便调用而写成类, 代码主要地方已有说明

硬件环境: 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 & " <a href=""" & surl & "1"">首页</a>"

sstrpageinfo = sstrpageinfo & " <a href=""" & surl & (sintpagenow – 1) & """>上一页</a>"

end if

if sintpagemax – sintpagenow < 1 then

sstrpageinfo = sstrpageinfo & " 下一页 末页 "

else

sstrpageinfo = sstrpageinfo & " <a href=""" & surl & (sintpagenow + 1) & """>下一页</a> "

sstrpageinfo = sstrpageinfo & " <a href=""" & surl & sintpagemax & """>末页</a> "

end if

sstrpageinfo = sstrpageinfo & " 页次:<strong><font color=""#990000"">" & sintpagenow & "</font> / " & sintpagemax & " </strong>"

sstrpageinfo = sstrpageinfo & " 共 <strong>" & sintrecordcount & "</strong> 条记录 <strong>" & sintpagesize & "</strong> 条/页 "

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

%>

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=gb2312">

<title>分页测试</title>

<style type="text/css">

<!–

.pageview {

font-size: 12px;

}

.pageview td {

border-right-style: solid;

border-bottom-style: solid;

border-right-color: #e0e0e0;

border-bottom-color: #e0e0e0;

border-right-width: 1px;

border-bottom-width: 1px;

}

.pageview table {

border-left-style: solid;

border-top-style: solid;

border-left-color: #e0e0e0;

border-top-color: #e0e0e0;

border-top-width: 1px;

border-left-width: 1px;

}

tr.header {

background: #eff7ff;

font-size: 14px;

font-weight: bold;

line-height: 120%;

text-align: center;

}

–>

</style>

<style type="text/css">

<!–

body {

font-size: 12px;

}

a:link {

color: #993300;

text-decoration: none;

}

a:visited {

color: #003366;

text-decoration: none;

}

a:hover {

color: #0066cc;

text-decoration: underline;

}

a:active {

color: #000000;

text-decoration: none;

}

table {

font-size: 12px;

}

–>

</style>

</head>

<body>

<table width="100%" border="0" cellspacing="0" cellpadding="4">

<tr>

<td>&nbsp;<%= strpageinfo%></td>

</tr>

</table>

<div class="pageview">

<table width="100%" border="0" cellspacing="0" cellpadding="4">

<tr class="header">

<td>id</td>

<td>描述</td>

<td>日期</td>

</tr>

<%

if isarray(arrrecordinfo) then

for i = 0 to ubound(arrrecordinfo, 2)

%>

<tr>

<td>&nbsp;<%= arrrecordinfo(0, i)%></td>

<td>&nbsp;<%= arrrecordinfo(1, i)%></td>

<td>&nbsp;<%= arrrecordinfo(2, i)%></td>

</tr>

<%

next

end if

%>

</table>

</div>

<table width="100%" border="0" cellspacing="0" cellpadding="4">

<tr>

<td>&nbsp;<%= strpageinfo%></td>

</tr>

</table>

<table width="100%" border="0" cellspacing="0" cellpadding="4">

<tr>

<td align="center">&nbsp;<%= gettimeover(1)%></td>

</tr>

</table>

</body>

</html>

这里将所有代码集中在一起是为了叙述和测试方便, 具体应用你可以将类放在单独一个文件, 然后多处调用. 请将适当的时候判断用户是否进行请求, 更改 intrefresh, 以便告知分页类是否重新统计记录数.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Access 2000 数据库 80 万记录通用快速分页类-ASP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章