我的一個asp + sqlserver的分頁程序:這個程序的特點就是指將當前頁的紀錄掉到頁面上來。性能不錯,而且不用存儲過程,由于代碼沒有封裝,所以很零散,要用到你的程序中,需要先簡單讀讀。然后注意需要自己設置的地方的意思。有些是可以省略的。有些參數傳遞也是可以省略的。 代碼如下: '''' 塞北的雪 分頁利器(sql server) 不用存儲過程 ------------------------- '其中注釋中有 ###的需要用戶設置 '其中注釋中有 參數傳遞 ** 的 說明要通過參數 傳遞。 '定義變量 dim tOption '查詢條件 dim tOrder '排序字符串 dim tOrderField '排序字段 可通過參數獲得:order_field dim tOrderDirection '排序方向 可通過參數獲得:order_direction
dim tPageSize '頁大小 dim tTotalCount '總記錄數 可通過參數獲得:t_count dim tPageCount '頁數 dim tCurPage '當前頁號 可通過參數獲得:page dim tTableName '表或者視圖名 dim tFieldAlias '行號的別名 dim tFieldList '查詢的字段列表 dim tPageField '用于分頁的字段 dim r_count '查得的記錄數 set rs=server.createobject("adodb.recordset") '記錄集對象
'排序處理 tOrderField=Request("order_field") '獲得排序字段(參數傳遞 **) tOrderDirection=Request("order_dir") '獲得排序方向(參數傳遞 **) if(tOrderField="") then tOrderField="item_code" ' ### 設置默認排序字段 if(tOrderDirection="") then tOrderDirection="asc" ' ### 設置默認排序方向 tOrder=" order by " & tOrderField & " " & tOrderDirection & " " '生成排序字符串 '定義參數 tPageSize=find_rs_count ' ### 設置頁大小 tTableName="view_select1" ' ### 設置與查詢的表格或視圖 tFieldList=" * " ' ### 欲查詢的字段列表 tPageField="item_code" ' ### 設置一個主鍵或唯一索引的字段 ,用于分頁計算
'頁數處理 tCurPage=Request("page") '獲得當前頁(參數傳遞 **) tTotalCount=Request("t_count") '獲得總頁數(參數傳遞 **)
if(tCurPage="") then tCurPage=1 if(cint(tCurPage)=0) then tCurPage=1 if(tPageCount="") then tPageCount =1 if(cint(tPageCount)=0) then tPageCount=1 ' 構造查詢條件,根據具體的程序,肯定不一樣。但是最后的條件必須是“ where ??? ” tOption=" issue_flag='Y'" ' ### 設置條件 if f_c<>"" then tOPtion= tOPtion & f_c ' ### 設置條件 if trim(tOption)="" then tOption = " where 1=1 " '如果沒有條件,就自己加一個。 else tOption= " where " & tOPtion end if '構造查詢字符串,這個分頁程序的核心,此查詢串是我們只下載當前頁所需的記錄 if(tCurPage>1) then conStr="select top " & tPageSize & " " & tFieldList & " from " & tTableName & tOption conStr =conStr & " and " & tPageField & " not in(select top " & tPageSize*(tCurPage-1) & " " & tPageField & " from " & tTableName & tOption & " " & tOrder & ") " & tOrder else conStr="select top " & tPageSize & " " & tFieldList & " from " & tTableName & tOption & " " & tOrder end if '執行主查詢,獲得相應記錄集 Call ConnDataBase() ' ### 建立數據庫連接 rs.cursorlocation=3 rs.open conStr,conn,3,1 '執行查詢 r_count= rs.recordcount '當還沒有查詢過總記錄數時 并且 總的記錄數超過了頁大小時 ,查詢當前條件下的總的記錄數 if(r_count>=tPageSize or tCurPage>1) and tTotalCount=0 then set rr=conn.execute("select count(*) from " & tTableName & " " & tOption) tTotalCount=rr(0) rr.close() set rr=nothing end if if(cint(tTotalCount)=0) then tTotalCount=r_count '如果總記錄為0,將當前差得的記錄集的記錄數設置為總記錄數,說明當前的總記錄數小于頁大小 '利用頁大小和總記錄數 計算頁數 if(cint(tTotalCount)>cint(tPageSize)) then tPageCount=cint((cint(tTotalCount) \ cint(tPageSize))) if(cint(tTotalCount) mod cint(tPageSize))>0 then tPageCount =tPageCount +1 end if end if tCurPage=cint(tCurPage) tPageCount=cint(tPageCount)
' ---------------------------------------------------------------------
這就是全部代碼,感興趣的朋友,可以研究一下,或者將他封裝起來,加上分頁導航等方法。總之,希望此代碼能對大家有用。
|