每次都在做分页,每次也都是写的相同的.今天早上自己想了想,查了些资料,ASP一般的分页方法有三种:
1.利用Recordset的属性PageSize,AbsolutePage设置来进行分页.
2.直接每次用Recodeset.Move PageSize来向前移动整页条记录.
3.利用SQL查询语言每次只取出页面数量的记录.

当然,最好效率的还有存储过程,这些原理差不多.

显然,1和2基本是一样的,每次取出全部记录到内存,这种方法的好处是简单明了,一般情况下使用最多,但是如果有大量记录时会大大降低效率.
3是最好的方法,每次取出pagesize记录,减少程序开销.

下面把自己写的1,3的方法贴一下:
---1--- 

<%
Dim conn,rs,i
Set conn=Server.CreateObject("ADODB.Connection")
conn.Open 
"Provider=SqlOleDb.1;Data Source=(local);User ID=sa;Password=;Initial Catalog=Pubs"
Set rs=Server.CreateObject("ADODB.Recordset")
rs.Open 
"SELECT au_id FROM authors ORDER BY au_id",conn,1,1,1
Dim myPageSize,currentPage,Total,myPageCount
myPageSize
=5
Total
=rs.RecordCount
If Total Mod myPagesize=0 Then
    myPageCount
=Int(Total/myPageSize)
Else
    myPageCount
=Int(Total/myPageSize)+1
End If
currentPage
=Request.QueryString("page")
If currentPage="" Or currentPage=Empty Then
    currentPage
=1
ElseIf currentPage>myPageCount+1 Then
    currentPage
=myPageCount
End If
rs.PageSize
=myPageSize
rs.AbsolutePage
=currentPage
For i=1 To myPageSize
    Response.Write(rs(
0))
    Response.Write(
"<br>")
    rs.MoveNext
    
If rs.EOF Then
        
Exit For
    
End If
Next
Response.Write(
"<br>")
For i=1 To myPageCount
    Response.Write(
"<a href=?page=" & i & ">" & i & "</a>")
    Response.Write(
" ")
Next
rs.Close
%
>

 


---3---  

<%
Dim conn,rs,i
Const myPageSize=5
Dim totalCount,currentPage,pageCount
Set conn=Server.CreateObject("ADODB.Connection")
conn.Open 
"Provider=SqlOleDb.1;Data Source=(local);User ID=sa;Password=;Initial Catalog=Pubs"
Set rs=conn.Execute("SELECT COUNT(au_id) FROM authors",,1)
totalCount
=rs(0)
rs.Close
If totalCount Mod myPageSize=0 Then
    pageCount
=totalCount/myPageSize
Else
    pageCount
=Int(totalCount/myPageSize)+1
End If
currentPage
=Request.QueryString("page")
If currentPage="" Or NOT IsNumeric(currentPage) Then
    currentPage
=1
Else
    currentPage
=Int(currentPage)
    
If currentPage>pageCount Then
        currentPage
=pageCount
    
End If
End If
If currentPage=1 Then
    
Set rs=conn.Execute("SELECT TOP " & myPageSize & " * FROM authors ORDER BY au_id")
Else
    
Set rs=conn.Execute("SELECT TOP " & myPageSize & " * FROM authors WHERE au_id NOT IN (SELECT TOP " & myPageSize*(currentPage-1& " au_id FROM authors ORDER BY au_id)")
End If
Response.Write(
"<div style='font-size:9pt'>")
For i=1 To myPageSize
    Response.Write(rs(
0& " " & rs(1& " " & rs(2& "<br>")
    rs.MoveNext
    
If rs.EOF Then Exit For End If
Next
Response.Write(
"" & currentPage & "页  共" & pageCount & "页  ")
If currentPage=1 Then
    Response.Write(
"<font color=gray>首页  上一页</font>  ")
Else
    Response.Write(
"<a href=?page=1>首页</a>  <a href=?page=" & currentPage-1 & ">上一页</a>  ")
End If
If currentPage=pageCount Then
    Response.Write(
"<font color=gray>下一页  尾页</font>  ")
Else
    Response.Write(
"<a href=?page=" & currentPage+1 & ">下一页</a>  <a href=?page=" & pageCount & ">尾页</a>  ")
End If
Response.Write(
"详细分页:")
For i=1 To pageCount
    Response.Write(
"<a href=?page=" & i & ">" & i & "</a>  ")
Next
Response.Write(
"</div>")
%
>
posted on 2005-06-27 11:51  wddavid  阅读(756)  评论(0)    收藏  举报