当前位置:asp编程网>技术教程>Asp教程>  正文

sql存储过程----分页显示表中的记录

1970-01-01 08:00:00   来源:本站原创    作者:佚名   浏览量:2571   收藏
通常在显示表中的记录的时候,由于记录很多,需要对记录进行分页显示,这里是利用sql存储过程进行分页显示
1、数据库和字段,conn.asp见
http://www.aspprogram.cn/detail.asp?id=35
请在表中添加几条记录

2、存储过程代码
    CREATE PROCEDURE dbo.getUserList
       @iPageCount int OUTPUT,   --总页数
       @iPage int,               --当前页号
       @iPageSize int            --每页记录数
    as
    set nocount on
    begin
       --创建临时表 
       create table #t (ID int IDENTITY,   --自增字段
                        y_id int,
                        y_username varchar(40),
                        y_password varchar(40))
       --向临时表中写入数据
       insert into #t 
          select y_id,y_username,y_password from dbo.[user]
             order by y_id desc
       
       --取得记录总数 
       declare @iRecordCount int
       set @iRecordCount = @@rowcount

       --确定总页数
       IF @iRecordCount%@iPageSize=0
          SET @iPageCount=CEILING(@iRecordCount/@iPageSize)
       ELSE
          SET @iPageCount=CEILING(@iRecordCount/@iPageSize)+1
 
       --若请求的页号大于总页数,则显示最后一页
       IF @iPage > @iPageCount
          SELECT @iPage = @iPageCount

       --确定当前页的始末记录
       DECLARE @iStart int    --start record
       DECLARE @iEnd int      --end record
       SELECT @iStart = (@iPage - 1) * @iPageSize
       SELECT @iEnd = @iStart + @iPageSize + 1

       --取当前页记录    
       select * from #t where ID>@iStart and ID<@iEnd

       --删除临时表
       DROP TABLE #t

       --返回记录总数
       return @iRecordCount
    end
GO


3、显示记录list2.asp
<!--#include file="conn.asp"-->
<%
'**调用分页存储过程**
    DIM pagenow,pagesize,pagecount,recordcount
    DIM MyComm,MyRst
    pagenow =Request("pn") 
    '自定义函数用于验证自然数
    if pagenow = "" then pagenow = 1
 pagenow=CInt(pagenow)
    pagesize = 2
    Set MyComm = Server.CreateObject("ADODB.Command")
    with MyComm
       .ActiveConnection = conn          'conn是数据库连接字串
       .CommandText      = "getUserList"     '指定存储过程名
       .CommandType      = 4                 '表明这是一个存储过程
       .Prepared         = true              '要求将SQL命令先行编译
       '返回值(记录总量) 
       .Parameters.Append .CreateParameter("RETURN",2,4)
       '出参(总页数)
       .Parameters.Append .CreateParameter("@iPageCount",3,2)
       '入参(当前页号)
       .Parameters.append .CreateParameter("@iPage",3,1,4,pagenow)
       '入参(每页记录数)
       .Parameters.append .CreateParameter("@iPageSize",3,1,4,pagesize)
       Set rs = .Execute
    end with
    if rs.state = 0 then        '未取到数据,rs关闭
       recordcount = -1
    else
       rs.close    '注意:若要取得参数值,需先关闭记录集对象
       recordcount = MyComm(0)
       pagecount   = CInt(MyComm(1))
       if cint(pagenow)>=cint(pagecount) then pagenow=pagecount
    end if
    Set MyComm = Nothing

    '以下显示记录
    if recordcount = 0 then
       Response.Write "无记录"
    elseif recordcount > 0 then
       rs.open
       Do While Not rs.eof
    response.write rs("y_id")&#38;":"&#38;rs("y_username")&#38;"----"&#38;rs("y_password")&#38;"<br>"
    rs.movenext
    loop     
   '*****************************分页代码**********************
       If pagenow>1 Then
        response.write "<a href=""?pn=1"">首页</a> "
    Else
     response.write "首页 "
    End If
    If pagenow>1 Then
      response.write "<a  href=""?pn="&#38;pagenow-1&#38;""">上一页</a> "
    Else
     response.write "上一页 "
    End If
    If pagenow<pagecount Then
      response.write "<a href=""?pn="&#38;pagenow+1&#38;""">下一页</a> "
    Else
      response.write "下一页 "
    End If
    If pagenow<pagecount Then
     response.write "<a href=""?pn="&#38;pagecount&#38;""">尾页</a> "
    Else
     response.write "尾页 "
    End if    
    else  'recordcount=-1
       Response.Write "参数错误"
    end If
'*********************结束*****************************
%>

4、

5、完成,运行list2.asp文件,就可以看到内容了,点击 首页,上一页,下一页,尾页来体验分页的感觉。

原创文件,转载请注明来源,作者。



关于我们-广告合作-联系我们-积分规则-网站地图

Copyright(C)2013-2017版权所属asp编程网