天天看点

用asp实现access数据库显示的分页功能

<%

dim rs

dim sql

msg_per_page = 2 '定义每页显示记录条数

set rs = server.createobject("adodb.recordset")

sql = "select * from c_txt order by id desc" '改成你自己的SQL语句

rs.cursorlocation = 3 '使用客户端游标,可以使效率提高

rs.pagesize = msg_per_page '定义分页记录集每页显示记录数

rs.open sql,conn,0,1

if err.number<>0 then '错误处理

response.write "数据库操作失败:" & err.description

err.clear

else

if not (rs.eof and rs.bof) then '检测记录集是否为空

totalrec = RS.RecordCount 'totalrec:总记录条数

if rs.recordcount mod msg_per_page = 0 then '计算总页数,recordcount:数据的总记录数

n = rs.recordcount/msg_per_page 'n:总页数

else

n = rs.recordcount/msg_per_page+1

end if

currentpage = request("page") 'currentpage:当前页

If currentpage <> "" then

currentpage = cint(currentpage)

if currentpage < 1 then

currentpage = 1

end if

if err.number <> 0 then

err.clear

currentpage = 1

end if

else

currentpage = 1

End if

if currentpage*msg_per_page > totalrec and not((currentpage-1)*msg_per_page < totalrec)then

currentPage=1

end if

rs.absolutepage = currentpage 'absolutepage:设置指针指向某页开头

rowcount = rs.pagesize 'pagesize:设置每一页的数据记录数

dim i

dim k

%>

<%

sub listPages()

if n <= 1 then exit sub

for i=0 to n/msg_per_page-1

for j=1 to msg_per_page

%>

<font size="2" color="#006600"> <a href="<%=request.ServerVariables(" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" script_name")%>?page=<%=i*msg_per_page+j %>">[

<% =i*msg_per_page+j %>

]</a></font>

<%

next

next

EndPage = n mod msg_per_page

for j=1 to EndPage

%>

<font size="2" color="#006600"> <a href="<%=request.ServerVariables(" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" script_name")%>?page=<% =i*msg_per_page+j %>">[

<% =i*msg_per_page+j %>

]</a></font>

<%next%>

<font color=black > 页数:<%=currentpage%>/<%=n%>页 <%=msg_per_page%>条/页 总记录数:<%=totalrec%>条数据</font>

<%end sub%>

<table align="center">

<tr>

<td align="center" valign="middle">

<%call listPages()%>

</td>

</tr>

</table>

<%do while not rs.eof and rowcount > 0%>

<table width="789" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

<tr bgcolor="#FFFFFF">

<td width="156" height="30" align="right" valign="middle">姓名:</td>

<td width="633"><%= rs("name") %> </td>

</tr>

<tr bgcolor="#FFFFFF">

<td height="30" align="right" valign="middle">性别:</td>

<td><%= rs("sex") %></td>

</tr>

<tr bgcolor="#FFFFFF">

<td height="30" align="right" valign="middle">内容:</td>

<td><%= rs("neirong") %></td>

</tr>

</table>

<%

rowcount=rowcount-1

rs.MoveNext

loop

end if

end if

rs.close

set rs=nothing

%> 

 ---------------------------------------------------------------------

方法2:

连接数据库就不写了,从正文开始:

<%

dim rs

dim sql

msg_per_page = 5 '定义每页显示记录条数

set rs = server.createobject("adodb.recordset")

sql = "select * from gbook order by id desc" '改成你自己的SQL语句

rs.cursorlocation = 3 '使用客户端游标,可以使效率提高

rs.pagesize = msg_per_page '定义分页记录集每页显示记录数

rs.open sql,conn,0,1 

if err.number<>0 then '错误处理

response.write "数据库操作失败:" & err.description

err.clear

else

if not (rs.eof and rs.bof) then '检测记录集是否为空

totalrec = RS.RecordCount 'totalrec:总记录条数

if rs.recordcount mod msg_per_page = 0 then '计算总页数,recordcount:数据的总记录数

n = rs.recordcount/msg_per_page 'n:总页数

else 

n = rs.recordcount/msg_per_page+1 

end if 

currentpage = request("page") 'currentpage:当前页

If currentpage <> "" then

currentpage = cint(currentpage)

if currentpage < 1 then 

currentpage = 1

end if 

if err.number <> 0 then 

err.clear

currentpage = 1

end if

else

currentpage = 1

End if 

if currentpage*msg_per_page > totalrec and not((currentpage-1)*msg_per_page < totalrec)then 

currentPage=1

end if

rs.absolutepage = currentpage 'absolutepage:设置指针指向某页开头

rowcount = rs.pagesize 'pagesize:设置每一页的数据记录数

dim i

dim k

%>

读数据从下面开始

<%do while not rs.eof and rowcount > 0 %>

写内容啦。。读数据!

<%

rowcount=rowcount-1

rs.MoveNext

loop

end if

end if

rs.close

set rs=nothing

%>

下面一句调用分页!

<%call listPages()%>

下面是定义的分页过程:

<%

sub listPages() 

if n <= 1 then exit sub 

%><p align=center>

<%if currentpage = 1 then%>[第一页]|[上一页]|<%else%> 

[<a href="<%=request.ServerVariables(" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" script_name")%>?page=1">第一页</a>]|[

 <a href="<%=request.ServerVariables(" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" script_name")%>?page=<%=currentpage-1%>">上一页</a>]

<%end if%>

<%if currentpage = n then%>|[下一页] | [最后一页]<%else%> 

|[<a href="<%=request.ServerVariables(" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" script_name")%>?page=<%=currentpage+1%>">下一页</a>]|

 [<a href="<%=request.ServerVariables(" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" script_name")%>?page=<%=n%>">最后一页</a>]

<%end if%>

当前第<%=currentpage%>页/共<%=n%>页,每页<%=msg_per_page%>条留言,共有<%=totalrec%>条留言.</p>

<%end sub%>