我的一个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” ### 设置默认排序方向
www.knowsky.com
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)
———————————————————————
这就是全部代码,感兴趣的朋友,可以研究一下,或者将他封装起来,加上分页导航等方法。总之,希望此代码能对大家有用。