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

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

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

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

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

 


---3---  

ASP分页一览<%
ASP分页一览
Dim conn,rs,i
ASP分页一览
Const myPageSize=5
ASP分页一览
Dim totalCount,currentPage,pageCount
ASP分页一览
Set conn=Server.CreateObject("ADODB.Connection")
ASP分页一览conn.Open 
"Provider=SqlOleDb.1;Data Source=(local);User ID=sa;Password=;Initial Catalog=Pubs"
ASP分页一览
Set rs=conn.Execute("SELECT COUNT(au_id) FROM authors",,1)
ASP分页一览totalCount
=rs(0)
ASP分页一览rs.Close
ASP分页一览
If totalCount Mod myPageSize=0 Then
ASP分页一览    pageCount
=totalCount/myPageSize
ASP分页一览
Else
ASP分页一览    pageCount
=Int(totalCount/myPageSize)+1
ASP分页一览
End If
ASP分页一览currentPage
=Request.QueryString("page")
ASP分页一览
If currentPage="" Or NOT IsNumeric(currentPage) Then
ASP分页一览    currentPage
=1
ASP分页一览
Else
ASP分页一览    currentPage
=Int(currentPage)
ASP分页一览    
If currentPage>pageCount Then
ASP分页一览        currentPage
=pageCount
ASP分页一览    
End If
ASP分页一览
End If
ASP分页一览
If currentPage=1 Then
ASP分页一览    
Set rs=conn.Execute("SELECT TOP " & myPageSize & " * FROM authors ORDER BY au_id")
ASP分页一览
Else
ASP分页一览    
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)")
ASP分页一览
End If
ASP分页一览Response.Write(
"<div style='font-size:9pt'>")
ASP分页一览
For i=1 To myPageSize
ASP分页一览    Response.Write(rs(
0& " " & rs(1& " " & rs(2& "<br>")
ASP分页一览    rs.MoveNext
ASP分页一览    
If rs.EOF Then Exit For End If
ASP分页一览
Next
ASP分页一览Response.Write(
"" & currentPage & "页  共" & pageCount & "页  ")
ASP分页一览
If currentPage=1 Then
ASP分页一览    Response.Write(
"<font color=gray>首页  上一页</font>  ")
ASP分页一览
Else
ASP分页一览    Response.Write(
"<a href=?page=1>首页</a>  <a href=?page=" & currentPage-1 & ">上一页</a>  ")
ASP分页一览
End If
ASP分页一览
If currentPage=pageCount Then
ASP分页一览    Response.Write(
"<font color=gray>下一页  尾页</font>  ")
ASP分页一览
Else
ASP分页一览    Response.Write(
"<a href=?page=" & currentPage+1 & ">下一页</a>  <a href=?page=" & pageCount & ">尾页</a>  ")
ASP分页一览
End If
ASP分页一览Response.Write(
"详细分页:")
ASP分页一览
For i=1 To pageCount
ASP分页一览    Response.Write(
"<a href=?page=" & i & ">" & i & "</a>  ")
ASP分页一览
Next
ASP分页一览Response.Write(
"</div>")
ASP分页一览%
>

相关文章: