服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP教程 - Access 2000 数据库 80 万记录通用快速分页类

Access 2000 数据库 80 万记录通用快速分页类

2019-10-21 10:21asp技术网 ASP教程

主要思路: 用一条语句统计(Count)出记录数(而不在查询时获得 RecordCount 属性), 缓存在 Cookies 中, 跳转时就不用再次统计. 使用 ADO 的 AbsolutePage 属性进行页面跳转即可. 为方便调用而写成类, 代码主要地方已有说明

代码本人优化过,测试通过

主要思路:用一条语句统计(Count)出记录数(而不在查询时获得RecordCount属性),缓存在Cookies中,跳转时就不用再次统计.使用ADO的AbsolutePage属性进行页面跳转即可.为方便调用而写成类,代码主要地方已有说明

硬件环境:AMDAthlonXP2600+,256DDR

软件环境:MSWindows2000AdvancedServer+IIS5.0+Access2000+IE6.0

测试结果:初次运行在250(首页)-400(末页)毫秒,(记录数缓存后)在页面间跳转稳定在47毫秒以下.第1页跳到最后一页不多于350毫秒

适用范围:用于普通分页.不适用于有较复杂的查询时:如条件为"[Title]Like’%最爱%’",查询的时间大大增加,就算Title字段作了索引也没用.:(

  1. <%  
  2. Dim intDateStart  
  3. intDateStart = Timer()  
  4.  
  5. Rem ## 打开数据库连接  
  6. Rem #################################################################  
  7. function f__OpenConn()  
  8. Dim strDbPath  
  9. Dim connstr  
  10. strDbPath = "fenye/db.mdb"  
  11. connstr  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="  
  12. connstr  = connstr & Server.MapPath(strDbPath)  
  13. Set conn  = Server.CreateObject("Adodb.Connection")  
  14. conn.open connstr  
  15. End function  
  16. Rem #################################################################  
  17.  
  18. Rem ## 关闭数据库连接  
  19. Rem #################################################################  
  20. function f__CloseConn()  
  21. If IsObject(conn) Then  
  22. conn.close  
  23. End If  
  24. Set conn = nothing  
  25. End function  
  26. Rem #################################################################  
  27. Rem 获得执行时间  
  28. Rem #################################################################  
  29. function getTimeOver(iflag)  
  30. Dim tTimeOver  
  31. If iflag = 1 Then  
  32. tTimeOver = FormatNumber(Timer() - intDateStart, 6, true)  
  33. getTimeOver = " 执行时间: " & tTimeOver & " 秒"  
  34. Else  
  35. tTimeOver = FormatNumber((Timer() - intDateStart) * 1000, 3, true)  
  36. getTimeOver = " 执行时间: " & tTimeOver & " 毫秒"  
  37. End If  
  38. End function  
  39. Rem #################################################################  
  40. Class Cls_PageView  
  41. Private sbooInitState  
  42. Private sstrCookiesName  
  43. Private sstrPageUrl  
  44. Private sstrPageVar  
  45. Private sstrTableName  
  46. Private sstrFieldsList  
  47. Private sstrCondiction  
  48. Private sstrOrderList  
  49. Private sstrPrimaryKey  
  50. Private sintRefresh  
  51.  
  52. Private sintRecordCount  
  53. Private sintPageSize  
  54. Private sintPageNow  
  55. Private sintPageMax  
  56.  
  57. Private sobjConn  
  58.  
  59. Private sstrPageInfo  
  60.  
  61. Private Sub Class_Initialize  
  62. Call ClearVars()  
  63. End Sub  
  64.  
  65. Private Sub class_terminate()  
  66. Set sobjConn = nothing  
  67. End Sub  
  68.  
  69. Public Sub ClearVars()  
  70. sbooInitState = False  
  71. sstrCookiesName = ""  
  72. sstrPageUrl = ""  
  73. sstrPageVar = "page"  
  74. sstrTableName = ""  
  75. sstrFieldsList = ""  
  76. sstrCondiction = ""  
  77. sstrOrderList = ""  
  78. sstrPrimaryKey = ""  
  79. sintRefresh = 0  
  80.  
  81. sintRecordCount = 0  
  82. sintPageSize = 0  
  83. sintPageNow = 0  
  84. sintPageMax = 0  
  85. End Sub  
  86.  
  87. Rem ## 保存记录数的 Cookies 变量  
  88. Public Property Let strCookiesName(Value)  
  89. sstrCookiesName = Value  
  90. End Property  
  91.  
  92. Rem ## 转向地址  
  93. Public Property Let strPageUrl(Value)  
  94. sstrPageUrl = Value  
  95. End Property  
  96.  
  97. Rem ## 表名  
  98. Public Property Let strTableName(Value)  
  99. sstrTableName = Value  
  100. End Property  
  101.  
  102. Rem ## 字段列表  
  103. Public Property Let strFieldsList(Value)  
  104. sstrFieldsList = Value  
  105. End Property  
  106.  
  107. Rem ## 查询条件  
  108. Public Property Let strCondiction(Value)  
  109. If Value <> "" Then  
  110. sstrCondiction = " WHERE " & Value  
  111. Else  
  112. sstrCondiction = ""  
  113. End If  
  114. End Property  
  115.  
  116. Rem ## 排序字段, 如: [ID] ASC, [CreateDateTime] DESC  
  117. Public Property Let strOrderList(Value)  
  118. If Value <> "" Then  
  119. sstrOrderList = " ORDER BY " & Value  
  120. Else  
  121. sstrOrderList = ""  
  122. End If  
  123. End Property  
  124.  
  125. Rem ## 用于统计记录数的字段  
  126. Public Property Let strPrimaryKey(Value)  
  127. sstrPrimaryKey = Value  
  128. End Property  
  129.  
  130. Rem ## 每页显示的记录条数  
  131. Public Property Let intPageSize(Value)  
  132. sintPageSize = toNum(Value, 20)  
  133. End Property  
  134.  
  135. Rem ## 数据库连接对象  
  136. Public Property Let objConn(Value)  
  137. Set sobjConn = Value  
  138. End Property  
  139.  
  140. Rem ## 当前页  
  141. Public Property Let intPageNow(Value)  
  142. sintPageNow = toNum(Value, 1)  
  143. End Property  
  144.  
  145. Rem ## 页面参数  
  146. Public Property Let strPageVar(Value)  
  147. sstrPageVar = Value  
  148. End Property  
  149.  
  150. Rem ## 是否刷新. 1 为刷新, 其他值则不刷新  
  151. Public Property Let intRefresh(Value)  
  152. sintRefresh = toNum(Value, 0)  
  153. End Property  
  154.  
  155. Rem ## 获得当前页  
  156. Public Property Get intPageNow()  
  157. intPageNow = singPageNow  
  158. End Property  
  159.  
  160. Rem ## 分页信息  
  161. Public Property Get strPageInfo()  
  162. strPageInfo = sstrPageInfo  
  163. End Property  
  164.  
  165. Rem ## 取得记录集, 二维数组或字串, 在进行循环输出时必须用 IsArray() 判断  
  166. Public Property Get arrRecordInfo()  
  167. If Not sbooInitState Then  
  168. Exit Property  
  169. End If  
  170.  
  171. Dim rs, sql  
  172. sql = "SELECT " & sstrFieldsList & _  
  173. " FROM " & sstrTableName & _  
  174. sstrCondiction & _  
  175. sstrOrderList  
  176.  
  177. Set rs = Server.CreateObject("Adodb.RecordSet")  
  178. rs.open sql, sobjConn, 1, 1  
  179. If Not(rs.eof or rs.bof) Then  
  180. rs.PageSize = sintPageSize  
  181. rs.AbsolutePage = sintPageNow  
  182. If Not(rs.eof or rs.bof) Then  
  183. arrRecordInfo = rs.getrows(sintPageSize)  
  184. Else  
  185. arrRecordInfo = ""  
  186. End If  
  187. Else  
  188. arrRecordInfo = ""  
  189. End If  
  190. rs.close  
  191. Set rs = nothing  
  192. End Property  
  193.  
  194. Rem ## 初始化记录数  
  195. Private Sub InitRecordCount()  
  196. sintRecordCount = 0  
  197. If Not(sbooInitState) Then Exit Sub  
  198. Dim sintTmp  
  199. sintTmp = toNum(request.Cookies("_xp_" & sstrCookiesName), -1)  
  200. If ((sintTmp < 0) Or (sintRefresh = 1))Then  
  201. Dim sql, rs  
  202. sql = "SELECT COUNT(" & sstrPrimaryKey & ")" & _  
  203. " FROM " & sstrTableName & _  
  204. sstrCondiction  
  205. Set rs = sobjConn.execute(sql)  
  206. If rs.eof or rs.bof Then  
  207. sintTmp = 0  
  208. Else  
  209. sintTmp = rs(0)  
  210. End If  
  211. sintRecordCount = sintTmp  
  212.  
  213. response.Cookies("_xp_" & sstrCookiesName) = sintTmp  
  214. Else  
  215. sintRecordCount = sintTmp  
  216. End If  
  217. End Sub  
  218.  
  219. Rem ## 初始化分页信息  
  220. Private Sub InitPageInfo()  
  221. sstrPageInfo = ""  
  222. If Not(sbooInitState) Then Exit Sub  
  223.  
  224. Dim surl     
  225. surl = sstrPageUrl     
  226. If Instr(1, surl, "?", 1) > 0 Then  
  227. surl = surl & "&" & sstrPageVar & "="  
  228. Else  
  229. surl = surl & "?" & sstrPageVar & "="  
  230. End If  
  231.  
  232. If sintPageNow <= 0 Then sintPageNow = 1  
  233. If sintRecordCount mod sintPageSize = 0 Then  
  234. sintPageMax = sintRecordCount \ sintPageSize  
  235. Else  
  236. sintPageMax = sintRecordCount \ sintPageSize + 1  
  237. End If  
  238. If sintPageNow > sintPageMax Then sintPageNow = sintPageMax  
  239.  
  240. If sintPageNow <= 1 then  
  241. sstrPageInfo = "首页 上一页"  
  242. Else  
  243. sstrPageInfo = sstrPageInfo & " <a href=""" & surl & "1"">首页</a>"  
  244. sstrPageInfo = sstrPageInfo & " <a href=""" & surl & (sintPageNow - 1) & """>上一页</a>"  
  245. End If  
  246.  
  247. If sintPageMax - sintPageNow < 1 then  
  248. sstrPageInfo = sstrPageInfo & " 下一页 末页 "  
  249. Else  
  250. sstrPageInfo = sstrPageInfo & " <a href=""" & surl & (sintPageNow + 1) & """>下一页</a> "  
  251. sstrPageInfo = sstrPageInfo & " <a href=""" & surl & sintPageMax & """>末页</a> "  
  252. End If  
  253.  
  254. sstrPageInfo = sstrPageInfo & " 页次:<strong><font color=""#990000"">" & sintPageNow & "</font> / " & sintPageMax & " </strong>"  
  255. sstrPageInfo = sstrPageInfo & " 共 <strong>" & sintRecordCount & "</strong> 条记录 <strong>" & sintPageSize & "</strong> 条/页 "  
  256. End Sub  
  257.  
  258. Rem ## 长整数转换  
  259. Private function toNum(s, Default)  
  260. s = s & ""  
  261. If s <> "" And IsNumeric(s) Then  
  262. toNum = CLng(s)  
  263. Else  
  264. toNum = Default  
  265. End If  
  266. End function  
  267.  
  268. Rem ## 类初始化  
  269. Public Sub InitClass()  
  270. sbooInitState = True  
  271. If Not(IsObject(sobjConn)) Then sbooInitState = False  
  272. Call InitRecordCount()  
  273. Call InitPageInfo()     
  274. End Sub  
  275. End Class  
  276.  
  277.  
  278. Dim strLocalUrl  
  279. strLocalUrl = request.ServerVariables("SCRIPT_NAME")  
  280.  
  281. Dim intPageNow  
  282. intPageNow = request.QueryString("page")  
  283.  
  284. Dim intPageSize, strPageInfo  
  285. intPageSize = 30  
  286.  
  287. Dim arrRecordInfo, i  
  288. Dim Conn  
  289. f__OpenConn  
  290. Dim clsRecordInfo  
  291. Set clsRecordInfo = New Cls_PageView  
  292.  
  293. clsRecordInfo.strTableName = "[table1]"  
  294. clsRecordInfo.strPageUrl = strLocalUrl  
  295. clsRecordInfo.strFieldsList = "[ID], [aaaa], [bbbb], [cccc]"  
  296. clsRecordInfo.strCondiction = "[ID] < 10000"  
  297. clsRecordInfo.strOrderList = "[ID] ASC"  
  298. clsRecordInfo.strPrimaryKey = "[ID]"  
  299. clsRecordInfo.intPageSize = 20  
  300. clsRecordInfo.intPageNow = intPageNow  
  301.  
  302. clsRecordInfo.strCookiesName = "RecordCount"  
  303. clsRecordInfo.strPageVar = "page"  
  304.  
  305. clsRecordInfo.intRefresh = 0  
  306. clsRecordInfo.objConn = Conn  
  307. clsRecordInfo.InitClass  
  308.  
  309. arrRecordInfo = clsRecordInfo.arrRecordInfo  
  310. strPageInfo = clsRecordInfo.strPageInfo  
  311. Set clsRecordInfo = nothing  
  312. f__CloseConn  
  313. %>  
  314. <html>  
  315. <head>  
  316. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
  317. <title>分页测试</title>  
  318. <style type="text/css">  
  319. <!--  
  320. .PageView {  
  321. font-size: 12px;  
  322. }  
  323. .PageView td {  
  324. border-right-style: solid;  
  325. border-bottom-style: solid;  
  326. border-right-color: #E0E0E0;  
  327. border-bottom-color: #E0E0E0;  
  328. border-right-width: 1px;  
  329. border-bottom-width: 1px;  
  330. }  
  331. .PageView table {  
  332. border-left-style: solid;  
  333. border-top-style: solid;  
  334. border-left-color: #E0E0E0;  
  335. border-top-color: #E0E0E0;  
  336. border-top-width: 1px;  
  337. border-left-width: 1px;  
  338. }  
  339. tr.Header {  
  340. background: #EFF7FF;  
  341. font-size: 14px;  
  342. font-weight: bold;  
  343. line-height: 120%;  
  344. text-align: center;  
  345. }  
  346. -->  
  347. </style>  
  348. <style type="text/css">  
  349. <!--  
  350. body {  
  351. font-size: 12px;  
  352. }  
  353. a:link {  
  354. color: #993300;  
  355. text-decoration: none;  
  356. }  
  357. a:visited {  
  358. color: #003366;  
  359. text-decoration: none;  
  360. }  
  361. a:hover {  
  362. color: #0066CC;  
  363. text-decoration: underline;  
  364. }  
  365. a:active {  
  366. color: #000000;  
  367. text-decoration: none;  
  368. }  
  369. table {  
  370. font-size: 12px;  
  371. }  
  372. -->  
  373. </style>  
  374. </head>  
  375. <body>  
  376. <table width="100%" border="0" cellspacing="0" cellpadding="4">  
  377.   <tr>  
  378.   <td> <%= strPageInfo%></td>  
  379. </tr>  
  380. </table>  
  381. <div class="PageView">  
  382.   <table width="100%" border="0" cellspacing="0" cellpadding="4">  
  383.     <tr class="Header">   
  384.     <td>ID</td>  
  385.     <td>描述</td>  
  386.     <td>日期</td>  
  387.   </tr>  
  388. <%  
  389.   If IsArray(arrRecordInfo) Then  
  390.    For i = 0 to UBound(arrRecordInfo, 2)  
  391. %>  
  392.   <tr>  
  393.     <td> <%= arrRecordInfo(0, i)%></td>  
  394.     <td> <%= arrRecordInfo(1, i)%></td>  
  395.     <td> <%= arrRecordInfo(2, i)%></td>  
  396.   </tr>  
  397. <%  
  398.    Next  
  399.   End If  
  400. %>  
  401. </table>  
  402. </div>  
  403. <table width="100%" border="0" cellspacing="0" cellpadding="4">  
  404.   <tr>   
  405.   <td> <%= strPageInfo%></td>  
  406. </tr>  
  407. </table>  
  408. <table width="100%" border="0" cellspacing="0" cellpadding="4">  
  409.   <tr>   
  410.     <td align="center"> <%= getTimeOver(1)%></td>  
  411.   </tr>  
  412. </table>  
  413. </body>  
  414. </html> 

 

 

 

 

 

 

 

 

延伸 · 阅读

精彩推荐