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

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

51Testing软件测试网0tN9Y]3@_
51Testing软件测试网#};f!uPa g
代码如下:
b c+Pm5XQ%SL6g8A0

51Testing软件测试网 F.p%\c)|pHy

''''   分页利器(sql server)  不用存储过程   -------------------------

FO\?hE0

5^1dlqf0'其中注释中有 ###的需要用户设置
)x n+YxX,j0'其中注释中有 参数传递 ** 的 说明要通过参数 传递。51Testing软件测试网R3^hr&I C{-\ }U

x9wM6N[S H!xt051Testing软件测试网pI)p8Xy
'定义变量51Testing软件测试网7e4_:URG Y P
dim tOption                  '查询条件
9PE9Lx'~KYRa/J0dim tOrder                   '排序字符串     
2n B7g,h p:I(E(p(Yj0dim tOrderField              '排序字段        可通过参数获得:order_field
.cEW5\vu{6DQ0dim tOrderDirection          '排序方向        可通过参数获得:order_direction

wCt2|h D051Testing软件测试网5`(YjykL@ T

dim tPageSize                '页大小51Testing软件测试网|b#IX&T~#^
dim tTotalCount              '总记录数        可通过参数获得:t_count
,L{h\0opi0dim tPageCount               '页数
`:o}v My`0dim tCurPage                 '当前页号        可通过参数获得:page51Testing软件测试网&xZ-Bs$n7F

G%^X|7D&H0dim tTableName               '表或者视图名
\:Q$J@1d7\0dim tFieldAlias              '行号的别名
Y0@QvPYW j `.kQ#W[0dim tFieldList               '查询的字段列表51Testing软件测试网.O w[6ta_5]!E6K,nE
dim tPageField               '用于分页的字段

{'O+gB1A$ew8|w0

R.rC]/ul0dim r_count                  '查得的记录数

H*Xx2g2Z^0

P.x8sa}i0
0jm iz0l-w G!cc0set rs=server.createobject("adodb.recordset")        '记录集对象51Testing软件测试网*T'D [l&U)FH v

f5z m7cXl4}0'排序处理
+zR ?zCx$k c0tOrderField=Request("order_field")                   '获得排序字段(参数传递 **)
u1?'iEar/?0tOrderDirection=Request("order_dir")                 '获得排序方向(参数传递 **)

QNG%~w0vut0

W]*i)i p't,^0if(tOrderField="") then tOrderField="item_code"       ' ### 设置默认排序字段51Testing软件测试网)Vyk&qeM7`P
if(tOrderDirection="") then tOrderDirection="asc"     ' ### 设置默认排序方向51Testing软件测试网I0Vn/@7P_'Y(z

Ji1rb w_w(EL*p#Q0tOrder=" order by " & tOrderField & " " & tOrderDirection & " "   '生成排序字符串51Testing软件测试网T4g'iy|_0B G

0XCcr| e4Q051Testing软件测试网+v1`%u9w5C
'定义参数51Testing软件测试网;L5T9h8gBo
tPageSize=find_rs_count        ' ### 设置页大小
\4H;C|oP0tTableName="view_select1"      ' ### 设置与查询的表格或视图51Testing软件测试网 H"o ^-Bi LJ HX
tFieldList=" * "               ' ### 欲查询的字段列表
`\jTj8Q1k(_0tPageField="item_code"         ' ### 设置一个主键或唯一索引的字段 ,用于分页计算51Testing软件测试网]O OG`/@

51Testing软件测试网7E9lnB u/m,ZX


~,V|'ga-h0'页数处理51Testing软件测试网|8\6f5x vn3Jd4t
tCurPage=Request("page")             '获得当前页(参数传递 **)
)Y*]|L MGd)_0tTotalCount=Request("t_count")       '获得总页数(参数传递 **)51Testing软件测试网{(QB|H/?

51Testing软件测试网+}0Kc#YUJcd&U

if(tCurPage="") then tCurPage=151Testing软件测试网pk0{*o+qOI
if(cint(tCurPage)=0) then tCurPage=151Testing软件测试网 _6r5B&Q;OD5m
if(tPageCount="") then tPageCount =1
~;H gS f6NU"n1R0if(cint(tPageCount)=0) then tPageCount=1

%yltv:`&M"ux0

5K/l$xONZJ6M0' 构造查询条件,根据具体的程序,肯定不一样。但是最后的条件必须是“ where ??? ”
#JQ4^v;e2Dr([} M0tOption=" issue_flag='Y'"                      ' ### 设置条件51Testing软件测试网Fm'aJ#AV5p
if f_c<>"" then tOPtion= tOPtion & f_c         ' ### 设置条件

{`XQs;^hFzcH$`0

;qd(|xG0if trim(tOption)="" then51Testing软件测试网@~ KO8B#tYn F
     tOption = " where 1=1 "   '如果没有条件,就自己加一个。
;]e H-P%j k rEN V0else
;LO }/QH3S Lq [0     tOption= " where " & tOPtion51Testing软件测试网)D+L6M9yK#M+G-n
end if

0{I*gPPj_|5Y0

#u5k kr6h9c0   51Testing软件测试网9J3sb"?-Jb5Y b
  
*E@9pM]8G!Xp0     '构造查询字符串,这个分页程序的核心,此查询串是我们只下载当前页所需的记录51Testing软件测试网0z+|hjWR!l/??y
 if(tCurPage>1) then
R3I6ahRJS%o_7X&B4oR0  conStr="select top " & tPageSize & " " & tFieldList & "  from  " & tTableName &  tOption
N+L'}m'p0  conStr =conStr & " and " & tPageField & " not in(select top " & tPageSize*(tCurPage-1) & " " & tPageField & "  from " & tTableName &  tOption & " " & tOrder & ") " & tOrder
1|j:V4]'fQ n;|4{.sOP0 else
ork*k'K:D*o/|g1U0  conStr="select top " & tPageSize & " " & tFieldList & " from " & tTableName & tOption & " " & tOrder
#g US!j6H!gUu0 end if 51Testing软件测试网 a-Bi1z4C UTR/Zs,k
 51Testing软件测试网/mL3{ pD4\O X9I
 51Testing软件测试网2}/h }9vO,g|[5G
 '执行主查询,获得相应记录集 51Testing软件测试网m)[x4Eg ]
 Call ConnDataBase()                     '  ### 建立数据库连接51Testing软件测试网$_r_D%L!@1Mo
    rs.cursorlocation=351Testing软件测试网a(db"Gy8R9{
    rs.open conStr,conn,3,1                 '执行查询
Y}6e cH%L0    r_count= rs.recordcount

qa4Cm/oG0

i7Vi,Z$j]%^0
0?#FWi%RYS&d/K0   '当还没有查询过总记录数时 并且 总的记录数超过了页大小时 ,查询当前条件下的总的记录数
P d4cM0Tv+]0   if(r_count>=tPageSize or tCurPage>1) and tTotalCount=0 then
qle4pO2k0      set rr=conn.execute("select count(*) from " & tTableName & " " & tOption)
u,}\6lq_;O9P/S0t0      tTotalCount=rr(0)
T` T&P E"x.l0      rr.close()51Testing软件测试网 d1X F:Rp;CX B
      set rr=nothing
4hgoiu0   end if   
kz6k+V$Q#S0   if(cint(tTotalCount)=0) then tTotalCount=r_count '如果总记录为0,将当前差得的记录集的记录数设置为总记录数,说明当前的总记录数小于页大小51Testing软件测试网&l mQX5m7WXi
  
zr:i*B9sL0   '利用页大小和总记录数 计算页数
/p%F3V9v*`6meE0   if(cint(tTotalCount)>cint(tPageSize)) then
1G^b-ex'XF!Fb0  tPageCount=cint((cint(tTotalCount) \ cint(tPageSize)))
4?#k g l A \msn;H0  if(cint(tTotalCount) mod cint(tPageSize))>0 then
&N|/T$d_3Zo0      tPageCount =tPageCount +151Testing软件测试网0MBY'`[:Q*V{ t
  end if
@e5Q!]Ho"D~P p,D0   end if
*H8gA {W,i z^ d0 51Testing软件测试网4k+u!c.f8e1^
   tCurPage=cint(tCurPage)
9~Z,w)E3v7C|0   tPageCount=cint(tPageCount)51Testing软件测试网,e p%xU$[.b+]V

51Testing软件测试网8gW N;S | B!TSF1}

51Testing软件测试网0hC!@ X!nf3k
'  ---------------------------------------------------------------------

6Zj+U C j%h&Y-W0

u8r U P+LO gK;^ J-y0 51Testing软件测试网l7pm K7Zy+s3w


TAG: ASP

 

评分:0

我来说两句

Open Toolbar