选择显示字体大小

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

        我的一个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)


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

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


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons