问答题 为了满足大量数据传输的需要,有时候需要把数据库中的数据分页传送,比如说,传送1000条数据时,可以一次传送10条数据,等到用户发出需求时再传送下10条数据,如此循环。试实现如下功能:只有当用户第—次点击脚本的页时,才从数据库中获取记录集,然后把记录集作为一个断开的记录集存储在一个Session变量中。这个Session变量用于在用户翻页时提供记录集。
【正确答案】程序清单: 1. < %@ LANGUAGE ="VBSCRIP"% > 2. <% 3. Qption Explicit 4. Response. Expires = 0 5. %> 6. <!--#include file ="adovbs. inc"--> 7. <~% 8. Const intPageSize =10 9. Dim imCurrentPage, objConn, objRS, strQ 10. Dim intTotalPages, intI 11. 12. If Request. ServerVariables("CONTENT_LENGTH"- 0 Then 13. intCurrentPage= 1 14. Else 15. intCurrentPage = Cint(Request. Form("Current Page")) 16. Select Case Request. Form("Submit") 17. Case "Previous" 18. intCurrentPage = intCurrentPage - 1 19. Case "Next" 20. intCurrentPage=intCurrentPage+1 21. End Select 22. Set objRS - Session ("MyRecol-dset") 23. End If 24. 25. If Not (isobjeet(objRS)) Then 26. Set objConn=Server. Createobject ("ADODB. Connection") 27. objConn. Open"Data Source = Northwind; User ID = sa; Password = 1" 28. 29. Set objRS =Server. CreateObject ("ADODB. Recordset") 30. objRS = CursodocatJon = adUseClient 31. objRS = CursorType = adOpenStatic 32. objRS = CaeheSize = intPageSize 33. strQ = "SELECT Customers. CompanyName, orders, orderDate" 34. strQ = strQ &"FROM Orders INNER JOIN Customers ON" 35. strQ = strQ &"Orders. CustomerlD- Customem CustomerlD" 36. strQ = strQ &"ORDER BY Orders. OrderDate," 37. strQ = strQ &"Customers. CompanyName" 38. objRS. Open strQ, objCom~,,, Nothing 39. Set objRS. ActiveConnction = Nothing 40. objConn. Close 41. Set objConn = Nothing 42. Set Session("MyRecordset") = objRS 43. End If 44. 45. objRS. PageSize = intPageSize 46. If Not(objRS. EOF) Then ohjRS. AbsolutePage = intCurrentPage 47. 48. intTotalPages = objRS. PageCount 49. %> 50. <HTML> <BODY> 51. <B> Customer:OrderDate</B><p> 52. <% 53. inti = 0 54. Do while((Not objRS. EOF) And (inti< ob}RS. PageSize)) 55. Response. Write Server. HTMLEncode (objRS("CompanyName") &:"&objRS("OrderDate")) &"<BR>" 56. inti=inti + 1 57. ohjRS. MoveNext 58. Loop 59. %> 60. <BR> 61. Page <% = intCurrentPage % > of < % = intTotalPages % > <7 p> 62. <FORM ACTION="<% =Request. ServerVariables("SCRIPT_NAME")%>" 63. <INPUT TYPE=>"Hidden"NAME="CvrrentPage"VALUE ="<% = intCurrentPage%> 64. <% 65. If intCurrentPage>1 Then %> 66. <INPUT TYPE = "Submit"NhME = "Submit"VALUE = "Previous"> 67. <% End If 68. If intCurrentPage00<>intTotalPages Then %> 69. <INPUT TYPE = "Submit'NAME = "SubmitVALUE = "Next"> 70. <% End If %> 71. </FORM> 72. </BODY></HTML> 注意储存在Session变量中的记录集在脚本中从没有关闭,因而,应该在810bal屈a中的SPS~lon的OtlEnd事件中完成这件事情: 1. <SCRIPT LANGUAGE = VBscript RUNAT = Server> 2. Sub Session OnEnd 3. Dim oh}RS 4. Set ob}RS=Session ('MyRecordset") 5. If isobject (objRS) Then 6. obojRS. Close 7. Set objRS = Nothing 8. Set Session ("MyRecordset") = Nothing 9. End If 10. End sub 11. </SCRIPT>
【答案解析】