如何用ASP+SQL Server实现分页功能(不用存储过程)

来源:Tech.acnow.net  作者:网络整理
摘要:我的一个asp+sqlserver的分页程序:这个程序的特点就是指将当前页的纪录掉到页面上来。性能不错,而且不用存储过程,由于代码没有封装,所以很零散,要用到你的程序中,需要先简单读读。然后注意需要自己设置的地方的意思。有些是可以省略的。有些参数传递也是可以省…

' 构造查询条件,根据具体的程序,肯定不一样。但是最后的条件必须是“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)

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

这就是全部代码,感兴趣的朋友,可以研究一下,或者将他封装起来,加上分页导航等方法。总之,希望此代码能对大家有用。

【相关文章】好搜一下
MySQL中文索引解决方案:二元分词法

MySQL中文索引解决方案:二元分词法

首先,我们来想想MySQL不支持中文索引的关键原因还是中文是双字节的,如果能把中…