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