|
原作:張立鋒 張 禾
概 述 現(xiàn)在有不少介紹利用ASP實(shí)現(xiàn)動(dòng)態(tài)分頁(yè)的文章,方法大同小異,就是每次利用ADO返回原始數(shù)據(jù)滿(mǎn)足條件記錄集中的指定頁(yè)。但在實(shí)際工程應(yīng)用中,原始數(shù)據(jù)量通常很大,原始數(shù)據(jù)的加工比較慢,如果每次換頁(yè)原始數(shù)據(jù)都要加工一次,則會(huì)嚴(yán)重影響應(yīng)用程序運(yùn)行的性能。 解決上述問(wèn)題主要有兩種途徑:一種途徑是將查詢(xún)條件相對(duì)固定,利用相對(duì)固定的查詢(xún)條件對(duì)原始數(shù)據(jù)進(jìn)行加工,生成一個(gè)小數(shù)據(jù)量的中間庫(kù),每次查詢(xún)都對(duì)中間庫(kù)進(jìn)行操作。這樣雖然會(huì)提高程序的性能,但會(huì)影響程序的靈活性,而且 Server端還需定時(shí)對(duì)原始數(shù)據(jù)進(jìn)行加工維護(hù)。另一個(gè)途徑是在Server端保存查詢(xún)的結(jié)果。這樣雖然不能改善查詢(xún)的性能,但Client端換頁(yè)時(shí)Server端能夠很快響應(yīng)。第一種途徑的實(shí)現(xiàn)比較簡(jiǎn)單,本文介紹第二種途徑的實(shí)現(xiàn)方法。
實(shí)現(xiàn)方法 將Server端的查詢(xún)結(jié)果保存在一個(gè)動(dòng)態(tài)數(shù)組中,即在 Session_OnStart過(guò)程中聲明一個(gè)二維的動(dòng)態(tài)數(shù)組。當(dāng)Server端收到Client端提交的申請(qǐng)后,首先判斷申請(qǐng)是條件查詢(xún)還是換頁(yè),如是條件查詢(xún)則判別查詢(xún)條件是否與上次提交的查詢(xún)條件不同,如不同則執(zhí)行查詢(xún),將查詢(xún)結(jié)果保存在該數(shù)組中,然后向Client端返回第一頁(yè)的內(nèi)容,否則直接從該數(shù)組中返回相應(yīng)頁(yè)的內(nèi)容。 程序?qū)崿F(xiàn) 1.定義二維數(shù)組及其他變量 Sub Session_OnStart dim TempDb() redim Preserve TempDb(1,2) session(“StoredArray") = TempDb ’定義一個(gè)Session數(shù)組 session(“iPageCount")=0 session(“iPageNo")=0 ...... End Sub
2.調(diào)用存儲(chǔ)過(guò)程返回?cái)?shù)據(jù)
Sub GetRecordSet(strBbmc,strKssj , strZzsj ,strNodeCode ,strFxzl ) '參數(shù)為報(bào)表名稱(chēng)和各個(gè)限制條件 select case strBbmc case “交易匯總表" strCnn=“PROVIDER=MSDASQL;dsn=sqldb; uid=sa;pwd=123456;database=vlog;" Set objcnn=Server.CreateObje(“ADODB.Connection") objcnn.CommandTimeout = 9999999 objcnn.ConnectionTimeout = 99999999 objcnn.CursorLocation = adUseClient objcnn.Open strCnn '打開(kāi)連接 Set objRs =Server.CreateObject (“ADODB.Recordset") objRS.PageSize = iPageSize objRS.CacheSize = iPageSize objRs.Open “sszhatmlog ‘“ & strKssj & "' , ‘“ & strZzsj & "', ‘“ & strNodeCode & "' , ‘“ & strFxzl & "'",objcnn,adOpenStatic , adLockReadOnly,1 ’執(zhí)行存儲(chǔ)過(guò)程返回查詢(xún)結(jié)果 ...... End Sub
3.將查詢(xún)結(jié)果保存到動(dòng)態(tài)數(shù)組
Sub SaveRecordSet() if objRs.EOF = false then objRs.movelast session(“iRowCount") = objRs.recordCount session(“iFieldCount") = objRs.Fields.Count session(“iPageCount") = objRs.pagecount redim Preserve TempArray(session (“iRowCount"),session(“iFieldCount")) ’TempArray是一個(gè)二維動(dòng)態(tài)數(shù)組, 根據(jù)記錄集大小重新定義其大小 objRs.MoveFirst iCount=0 do while objRs.EOF=false iCount = iCount + 1 for i= 1 to session(“iFieldCount") TempArray(iCount,i)=objRs.Fields.Item (i-1).value next objRs.MoveNext loop session(“StoredArray") = TempArray objRs.Close else session(“iPageCount") = 0 end if End Sub
4.顯示記錄內(nèi)容
Sub ShowRecord() ...... LocalArray=session(“StoredArray") iShowTotal=(iPageCurrent-1)*iPageSize+1 iRowLoop = 1 do while iRowLoop < = iPageSize and iShowTotal < = session(“iRowCount") Response.Write(“< TR >") for i = 1 To session(“iFieldCount") Response.write(“< TD >" & LocalArray(iShowTotal,i)) Next Response.Write(“< /TR >") iShowTotal = iShowTotal + 1 iRowLoop = iRowLoop + 1 loop Response.Write(“< /TABLE >") if iPageCurrent < > 1 and iPageCurrent < session (“iPageCount") then % > < center >< A HREF=“db_pag.asp?page=< %= iPageCurrent - 1 % >" >前一頁(yè)< /A >< A HREF= “db_pag.asp?page=< %= iPageCurrent + 1 % >" > 后一頁(yè)< /A >< /center > < % else if iPageCurrent < > 1 then % > < center >< A HREF=“db_pag.asp?page=< %= iPageCurrent - 1 % >" >前一頁(yè) < /A >< /center > < % end if if iPageCurrent < session(“iPageCount")then % > < center >< A HREF=“db_pag.asp?page= < %= iPageCurrent + 1 % >" >后一頁(yè) < /A > < /center > < % end if end if End Sub
5.主程序
if Request.QueryString(“page") = “" then ’提交查詢(xún)申請(qǐng)并且查詢(xún)條件與上一次不同 ...... call GetRecordSet(strBbmc,strKssj,strZzsj, strNodeCode,strFxzl) call SaveRecordSet Else iPageCurrent=CInt(Request.QueryString(“page")) strKssj=session(“strKssj") end if if session(“iPageCount") = 0 then Response.Write “抱歉!沒(méi)有滿(mǎn)足條件的記錄" Response.Write “< Br >" else call showrecord() end if
結(jié)束語(yǔ) 本程序的關(guān)鍵在于Session數(shù)組的定義及其賦值的實(shí)現(xiàn),通過(guò)應(yīng)用Session數(shù)組可以提高處理大量數(shù)據(jù)的應(yīng)用程序的性能。
|