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