喜欢本站并加入收藏
首 页 电脑学园 技术文档 图形图像 办公应用 网页设计 管理资料 PPT模板 方案合同
 位置: 北方教程网 >> 网页设计 >> Asp专题 >> 正文
   asp + sqlserver 分页方法详解
asp + sqlserver 分页方法详解
[ 作者:佚名   来源:网络整理   点击数:   更新时间:2007-2-18 ]
代码如下:

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


'定义变量
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)


'  ---------------------------------------------------------------------

        这就是全部代码,感兴趣的朋友,总之,希望此代码能对大家有用。

上一篇文章: 详解ASPImage组件制作水印过程 下一篇文章: ASP无组件上传进度条解决方法
打印此文  收藏此页  关闭窗口  返回顶部
 相关文章  热点文章
 · 利用CSS改进网站设计的小技巧
 · Dreamweaver快捷操作技巧集锦
 · 使用Dreamweaver架设网站简明教程
 · 网页设计制作过程中常用的几个代码技巧
 · 高效使用CSS编码的方法
 
热点图文
教你节省Vist...
使用Windows ...
 · 远程管理Web服务器的方法技巧
 · Win Vista系统自带IIS7.0设置详解...
 · 详解Windows系统IIS6.0功能及应用...
 · 服务器集群系统实现方法及案例分...
 · IIS服务器实现asp,cgi和php+mysq...
 · IIS下SQL Server数据库安全设置技...
推 荐 阅 读
巧用Photosho...
Photoshop鼠绘...
 · Fireworks制作旋转的漂亮gif动画...
 · Photoshop制作都市满天繁星
 · Fireworks制作Menu按钮
 · 巧用Photoshop制作喜迎奥运壁纸
 · 巧用Photoshop钢笔工具绘制彩色美...
 · Photoshop制作浪漫动感花朵
 设为首页 - 加入收藏 - 关于本站 - 联系我们 - 广告合作 - 友情链接 - 在线交流 - 
CopyRight© 2006-2009 Eduai.Com All Rights Reserved
在线交流 QQ:524152258 辽ICP备07001851号
免责声明:本站部分资源来自网络,如有侵犯您的版权请通知,我们立即删除。