正文

sql存储过程----分页显示表中的记录2007-10-31 14:32:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/wangsdong/30628.html

分享到:

通常在显示表中的记录的时候,由于记录很多,需要对记录进行分页显示,这里是利用sql存储过程进行分页显示1、数据库和字段,conn.asp见http://www.programfan.com/blog/article.asp?id=30476请在表中添加几条记录 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    endGO 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")&":"&rs("y_username")&"----"&rs("y_password")&"<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="&pagenow-1&""">上一页</a> "    Else     response.write "上一页 "    End If    If pagenow<pagecount Then      response.write "<a href=""?pn="&pagenow+1&""">下一页</a> "    Else      response.write "下一页 "    End If    If pagenow<pagecount Then     response.write "<a href=""?pn="&pagecount&""">尾页</a> "    Else     response.write "尾页 "    End if        else  'recordcount=-1       Response.Write "参数错误"    end If'*********************结束*****************************%> 4、 5、完成,运行list2.asp文件,就可以看到内容了,点击 首页,上一页,下一页,尾页来体验分页的感觉。原创文件,转载请注明来源,作者。

阅读(4527) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册