|
Include ADOVBS.INC to your ASP page. Don't have it? Do a Google for it and you should be able to download a copy. Note:
<!-- #include file="adovbs.inc" -->
Then, set up your DSN and open your connection to your database:
<%
strDSN = "DRIVER=SQL Server;SERVER=SQLSERVER;UID=someusername;PWD=somep assword;"
strDSN = strDSN & "APP=Microsoft® Windows® Operating System;WSID=WEB;DATABASE=SomeDatabase"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open strDSN, "someusername", "somepassword"
%>
Now, you need to set up your recordset options:
<%
Dim iPageSize 'How big our pages are
Dim iPageCount 'The number of pages we get back
Dim iPageCurrent 'The page we want to show
Dim strOrderBy 'A fake parameter used to illustrate passing them
Dim iRecordsShown 'Loop controller for displaying just iPageSize records
Dim I 'Standard looping var
' Get parameters
iPageSize = 10 ' You could easily allow users to change this
' Retrieve page to show or default to 1
If Request.QueryString("page") = "" Then
iPageCurrent = 1
Else
iPageCurrent = CInt(Request.QueryString("page"))
End If
%>
Set up your SQL statement and open the recordset in this manner:
<%
SQL = "SELECT * FROM MyDataBase"
Set RS = Server.CreateObject("ADODB.Recordset")
RS.PageSize = iPageSize
RS.CacheSize = iPageSize
RS.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
iPageCount = RS.PageCount
If iPageCurrent > iPageCount Then iPageCurrent = iPageCount
If iPageCurrent < 1 Then iPageCurrent = 1
%>
Finally, loop through your recordset in this manner:
<%
If Not RS.EOF Then
RS.AbsolutePage = iPageCurrent
iRecordsShown = 0
Do While iRecordsShown < iPageSize And Not RS.EOF
' blah blah, blah, blah, blah
' write out your table, etc as you normally would
iRecordsShown = iRecordsShown + 1
RS.MoveNext
Loop
End If
Set RS = Nothing
%>
And last, but not least, you can dump out some page numbers and navigation (replace scriptname.asp with the appropriate filename):
<%
' show 'Prev' if relevant
If iPageCurrent > 1 Then
%>
<A href="script.asp?page=<%= iPageCurrent - 1 %>">[<< Prev]</A>
<%
End If
' show page numbers:
For I = 1 To iPageCount
If I = iPageCurrent Then
%>
<%= I %>
<%
Else
%>
<A href="script.asp?page=<%= I %>"><%= I %></A>
<%
End If
Next 'I
' show 'Next' if relevant
If iPageCurrent < iPageCount Then
%>
<A href="script.asp?page=<%= iPageCurrent + 1 %">[Next >>]</A>
<%
End If
%>
Problems, questions? PM me!
Joe
|