asp + sqlserver 分页方法(不用存储过程)

上一篇 / 下一篇  2007-05-08 10:03:17 / 个人分类:ASP


!l$q3C9Q~$[4M8EV051Testing软件测试网{,p h} wK0| ^
代码如下:51Testing软件测试网2R3X*B)T"xI

51Testing软件测试网;Wk q}9Ab l

''''   分页利器(sql server)  不用存储过程   -------------------------51Testing软件测试网5yK1{-BHX

51Testing软件测试网 w(x*A9o \@

'其中注释中有 ###的需要用户设置
2U*[%H#XR0'其中注释中有 参数传递 ** 的 说明要通过参数 传递。

+H$]#taW4O6y051Testing软件测试网s SQ1dT


#z!Xc/r8G0'定义变量
-U2Vx6Ae ^:Ks2p0dim tOption                  '查询条件51Testing软件测试网\sQ{(alY&S
dim tOrder                   '排序字符串     51Testing软件测试网};lUmrX
dim tOrderField              '排序字段        可通过参数获得:order_field51Testing软件测试网[-W7Ec7Xp(H&|0uF
dim tOrderDirection          '排序方向        可通过参数获得:order_direction

0n B b A$qqO?051Testing软件测试网qBWf ZWB

dim tPageSize                '页大小51Testing软件测试网9NJQ x#G|m%X'yB
dim tTotalCount              '总记录数        可通过参数获得:t_count
U4qIuV0dim tPageCount               '页数51Testing软件测试网G:V d{C/JI7Z ^
dim tCurPage                 '当前页号        可通过参数获得:page

-LEed+d_051Testing软件测试网Jbpq&f]

dim tTableName               '表或者视图名51Testing软件测试网oi ]m CI$q
dim tFieldAlias              '行号的别名51Testing软件测试网@3D_8bjCsNx J.Y,@
dim tFieldList               '查询的字段列表51Testing软件测试网/K-aad9b?D
dim tPageField               '用于分页的字段

Y.Jy.y1G(sDM0

l3bY8g&GJ4P&tX4L0dim r_count                  '查得的记录数

9A]8O@x4~w DG0

.im/w)i} U3^0
E5[6\{1mT'N;C0set rs=server.createobject("adodb.recordset")        '记录集对象51Testing软件测试网5ve8v{}.Mw;KWj

K,],rcu2w Q4?)^K0'排序处理
P)txV8X.Q?0tOrderField=Request("order_field")                   '获得排序字段(参数传递 **)51Testing软件测试网N7L6i5V.y*yc
tOrderDirection=Request("order_dir")                 '获得排序方向(参数传递 **)

;VI7]E%Ki#a051Testing软件测试网T,Z/q&aH

if(tOrderField="") then tOrderField="item_code"       ' ### 设置默认排序字段51Testing软件测试网2qs(cM/IALPy
if(tOrderDirection="") then tOrderDirection="asc"     ' ### 设置默认排序方向

D^/w8\BSWm0

!s2t3i5p,g0tOrder=" order by " & tOrderField & " " & tOrderDirection & " "   '生成排序字符串

vcN?.?8@*bv0

n[.I*e|)F~j0
#X)F|.t }hG0'定义参数
P _ @4h-R0tPageSize=find_rs_count        ' ### 设置页大小
w/X9jgHtd:qA w0tTableName="view_select1"      ' ### 设置与查询的表格或视图51Testing软件测试网j/w qe q
tFieldList=" * "               ' ### 欲查询的字段列表
~C\V+K/d&[+|0tPageField="item_code"         ' ### 设置一个主键或唯一索引的字段 ,用于分页计算

W9S4K#t? o051Testing软件测试网B O"H5vH#P


N#PuaMZ"L}0'页数处理
-eZHU'Xsc P0tCurPage=Request("page")             '获得当前页(参数传递 **)51Testing软件测试网 M#@[s-~i4@7U Ji
tTotalCount=Request("t_count")       '获得总页数(参数传递 **)51Testing软件测试网 y(@*CX7X3?]{

&~ sHL ] }8\(C0if(tCurPage="") then tCurPage=1
YQ!m`k"Ls;ZgJ3J0if(cint(tCurPage)=0) then tCurPage=151Testing软件测试网 A:P { Q2M | n P&hc
if(tPageCount="") then tPageCount =151Testing软件测试网m| K@ bu~
if(cint(tPageCount)=0) then tPageCount=1

D,HC!zv051Testing软件测试网%P*t0]RD$`C{)qQ~

' 构造查询条件,根据具体的程序,肯定不一样。但是最后的条件必须是“ where ??? ”
b4eC:VX+Q~ i3Fo0tOption=" issue_flag='Y'"                      ' ### 设置条件
OE2Q/|D;s UU&M,a0if f_c<>"" then tOPtion= tOPtion & f_c         ' ### 设置条件51Testing软件测试网dHH,\!S w:l5DG

9m"\o{/s8Zb0if trim(tOption)="" then
S_z'pQ)Kp&?0     tOption = " where 1=1 "   '如果没有条件,就自己加一个。
)s2n;Y%ffR'Q0else
@Mr7e ]:m0     tOption= " where " & tOPtion
Y ES,u$o&p} Iw0end if51Testing软件测试网+z*y)LHD;P

51Testing软件测试网/e NW0| _sh"c4g

   51Testing软件测试网.Em!v7ql p#g i(yZ5`
  
Bu ]\(J,UX0H5T"B0     '构造查询字符串,这个分页程序的核心,此查询串是我们只下载当前页所需的记录
KX!Jg!jXX f/~0 if(tCurPage>1) then
*IwzW!V k0  conStr="select top " & tPageSize & " " & tFieldList & "  from  " & tTableName &  tOption51Testing软件测试网px~i h"d
  conStr =conStr & " and " & tPageField & " not in(select top " & tPageSize*(tCurPage-1) & " " & tPageField & "  from " & tTableName &  tOption & " " & tOrder & ") " & tOrder
z'CLV"{0 else
6C,b1{7qA0  conStr="select top " & tPageSize & " " & tFieldList & " from " & tTableName & tOption & " " & tOrder51Testing软件测试网Q6k1X)wjEY
 end if 
/q8Kn TJn {+a0 
.RPZ3S9h7C9B6VD&J j0 
L&}dgzL\0 '执行主查询,获得相应记录集 51Testing软件测试网[)c(E jRE Dt&y
 Call ConnDataBase()                     '  ### 建立数据库连接
Xu)_?W gU&S1G/q0    rs.cursorlocation=351Testing软件测试网4REw2`!C2G Mn6W
    rs.open conStr,conn,3,1                 '执行查询51Testing软件测试网 hb)E rcB)ol'X
    r_count= rs.recordcount

"a,s])s)G.{[#M0

n-Cfy*p0
T5B-IE0Y0   '当还没有查询过总记录数时 并且 总的记录数超过了页大小时 ,查询当前条件下的总的记录数51Testing软件测试网Vab7ah"l*G
   if(r_count>=tPageSize or tCurPage>1) and tTotalCount=0 then51Testing软件测试网y&WqqWd f
      set rr=conn.execute("select count(*) from " & tTableName & " " & tOption)51Testing软件测试网R4`W*F-i
      tTotalCount=rr(0)51Testing软件测试网g[-]D6yZx)v.ML6l5Q
      rr.close()
ti y.zG/y0      set rr=nothing
E"VX!h+wE\0   end if   
3w7d/X3HD:~Ad0   if(cint(tTotalCount)=0) then tTotalCount=r_count '如果总记录为0,将当前差得的记录集的记录数设置为总记录数,说明当前的总记录数小于页大小
X(~cdf:q@@0  51Testing软件测试网)u9H9F,k L&] M'j.J
   '利用页大小和总记录数 计算页数
I9Fw4Y G^F?.m EA;k0   if(cint(tTotalCount)>cint(tPageSize)) then51Testing软件测试网/KT u#T6Pp
  tPageCount=cint((cint(tTotalCount) \ cint(tPageSize)))
g!?a:H#a?:i0  if(cint(tTotalCount) mod cint(tPageSize))>0 then
|:^b K'z M0      tPageCount =tPageCount +1
Y3N%fn R0  end if51Testing软件测试网H,T |{ i2X G/]!u
   end if
`,X4l;muq%c1j0 
S(@:Pn s'hK:DUY0   tCurPage=cint(tCurPage)51Testing软件测试网uP sI$|%Ig G
   tPageCount=cint(tPageCount)

b4B4yQ)w6L3_0

6S)K)UQCz8q-\051Testing软件测试网6u l Q9|0h[-hOPI
'  ---------------------------------------------------------------------

+K rW,x9Y+I1n-U t0

2mgyD~bF4B0 

+uf8jr&F @ r*m+o0

TAG: ASP

 

评分:0

我来说两句

Open Toolbar