Code of the "infinite stream" Paging program in asp

Source: Internet
Author: User

<%
'*************************************** ***************************
'** Program name: "infinite stream" Paging Program
*** Author: Arbiter (AAsx)
'** Version: Million Level
'**
'** QQ: 22222xx
'** Email: Arbiter@21cn.com
'** Http://www.imagecity.org/
'*************************************** ***************************
'**
'** [Author's remarks]
'**
'** The paging program is undoubtedly a troublesome feature of many network programs. In fact
'** Most people are still using the traditional paging method (Rs. PageSize = xx ).
'** Database operators all know that this traditional method has a drawback: when you open the page for the first time,
'** It will pre-read all the record sets, which will be fatal when the data is big, and
'** The page turning speed will also be very slow, occupying resources. For data of more than 100,000 orders of magnitude
'** The traditional paging method of the library has become very weak, let alone a million-level (it cannot be operated at all
). For this reason, I started the program.
'**
'** [Program function]
'**
'** It performs paging operations on large databases, and the ideal number of operable data records is 2 million.
'** Or less (Max Level edition has no limit on the number of pages, regardless of the database size.
'** Unchanged). This is the paging program of the Million Level edition in the race Yang 1G, memory 512, win2k ring.
'** Test data:
'**
'** SQLserver 2 k + 0.1 million records + 20 records per page:
*** Average page turning speed: 45 ms
'** SQLserver 2 k + 1 million records + 20 records per page:
*** Average page turning speed: 350 ms
'**
'**
'** [Paging principle]
'**
'** This program no longer uses the Rs. PageSize Method for paging. the type of the cursor connecting to the database
'** Does not use conn, 1, x, but conn,. This should be the fastest cursor type.
'** Think this will complicate the program. On the contrary, the program is very simple. If you cannot understand it,
'** It should be my programming style that you are not used to, rather than complicated programs.
The center of the '** "infinite stream" page is that only the records to be displayed are read on each page.
'** The paging program prereads all the data, which is the biggest advantage of this program-less resources, the same
'** The processing speed has also been greatly improved, especially when the data volume is large, its speed advantage
'** The more obvious (1 million records are about ms ).
'** When the program is executed, use CurcorBegin and CurcorEnd to record the first entry
*** The ID value of the record and the last record as the marker for the next page flip, and then use Top xx to obtain
'** Displays the required data and records the ID value.
'**
'** [Statement]
'**
'** This program is a shared version and is provided to programmers for research and use.
*** For modifying or other purposes, please respect the author's hard work and indicate the source.
'** If the program has defects such as errors and omissions or non-optimization, go to www.csdn.net Web development/
'** The ASP topic is discussed. Please do not stick to it for the sake of the development of the Chinese Software Industry :)
'**
'*************************************** *****************************
Option Explicit
'Response. Flush
Dim BeginTime, EndTime
BeginTime = Timer
Dim conn, SQLstr, Rs, DefRecordNum, CursorBegin, CursorEnd, CurPageNum, hav
DefRecordNum = 20
'-------------- Get the relevant parameter ----------
If Request ("CursorBegin") = "" Then CursorBegin = 0 Else CursorBegin = Request ("CursorBegin ")
If Request ("CursorEnd") = "" Then CursorEnd = 0 Else CursorEnd = Request ("CursorEnd ")
If Request ("CurPageNum") <> "" Then
CurPageNum = CLng (Request ("CurPageNum "))
If CurPageNum <= 0 Then CurPageNum = 1
Else
CurPageNum = 1
End If
Hav = Request ("hav ")
If hav = "" Then hav = "next"
'---------------- End -----------------
'------------ Function for displaying paging content --------
Function TurnPageFS (DispRecordNum)
Dim n
While Not (Rs. Eof) And n <DispRecordNum
N = n + 1
Response. Write "<tr> "&_
"<Td bgcolor = 'efefef'>" & Rs (0) & "</td> "&_
"<Td bgcolor = 'efefef'>" & Rs (1) & "</td> "&_
"<Td bgcolor = 'efefef'>" & Rs (2) & "</td> "&_
"<Td bgcolor = 'efefef'>" & Rs (3) & "</td> "&_
"<Td bgcolor = 'efefef'>" & Rs (4) & "</td> "&_
"<Td bgcolor = 'efefef'>" & Rs (5) & "</td> "&_
"</Tr>"
If n = 1 Then CursorBegin = Rs (0)
If n = DefRecordNum or Rs. Eof Then CursorEnd = Rs (0)
Rs. MoveNext
Wend
End Function
'------------- Connect to the database -------------
Set conn = Server. CreateObject ("Adodb. Connection ")
'Sqlstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Server. Mappath ("mldata. mdb ")
SQLstr = "Driver = {sqlserver}; Server = arbiter; uid = arbiter; pwd = 123456; database = mldata"
Conn. Open SQLstr
'--------- Total number of records/total number of pages ---------
'-PS: count (ID) is recommended. The ID is automatically numbered and indexed. Otherwise, the speed may be compromised.
'-PS: This is the most resource-consuming part of the program. If this program is canceled, the speed will be about 10 times faster.
Dim TotalRecords, TotalPages
SQLstr = "Select count (ID) As RecordSum From ABC"
Set Rs = conn. Execute (SQLstr, 0, 1)
TotalRecords = Rs ("RecordSum ")
TotalPages = Abs (Int (TotalRecords/DefRecordNum * (-1 )))
Rs. Close
Set Rs = Nothing
'-------- Select the corresponding SQL string according to hav -----
Select Case (hav)
Case "back"
CursorEnd = CursorBegin
SQLstr = "Select Top" & DefRecordNum &"_
ID, Title, FileName, K, ImgSize, NameSon _
From ABC Where ID <"& CursorBegin &_
"And ID In (Select Top" & DefRecordNum _
& "ID From ABC Where ID <" & CursorBegin _
& "Order by id desc) order by ID"
Case "next"
SQLstr = "Select Top" & DefRecordNum _
& "ID, Title, FileName, K, ImgSize, NameSon From ABC Where ID>" & CursorEnd &_
"Order by ID"
End Select
Set Rs = conn. Execute (SQLstr, 0, 1)
%>
<Html>
<Head>
<Title> "infinite stream" Paging program Author: Arbiter </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Style type = "text/css"> td, br, div, p, body {font-size: 12px} </style>
</Head>
<Body bgcolor = "# FFFFFF" leftmargin = "0" topmargin = "0">
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "3" bgcolor = "# E2F5FE">
<Tr align = "center">
<Td colspan = "2"> <% Response. Write CurPageNum & "/" & TotalPages & "Total number of page records:" & TotalRecords %> </td>
<Td> <a href = "mllist. asp"> homepage </a> <a href = javascript: turnpage ('back');> previous page </a>
<A href = javascript: turnpage ('Next');> next page </a> </td>
</Tr>
</Table>
<Table width = "100%" border = "1" cellspacing = "0" cellpadding = "3" bgcolor = "# CCCCCC">
<Tr>
<Td> ID </td>
<Td> Title </td>
<Td> FileName </td>
<Td> size </td>
<Td> size </td>
<Td> Category </td>
</Tr>
<%
TurnPageFS (DefRecordNum)
Rs. Close
Set Rs = Nothing
Conn. Close
Set conn = Nothing
%>
</Table>
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "3" bgcolor = "# E2F5FE">
<Tr align = "center">
<Td colspan = "2"> <% Response. Write CurPageNum & "/" & TotalPages & "Total number of page records:" & TotalRecords %> </td>
<Td> <a href = "mllist. asp"> homepage </a> <a href = javascript: turnpage ('back');> previous page </a>
<A href = javascript: turnpage ('Next');> next page </a> </td>
</Tr>
</Table>
<%
EndTime = Timer
Response. Write "<br> program execution time:" & (EndTime-BeginTime) * 1000 & "millisecond"
Response. Write "the ID value (CursorBegin) of the first record =" & CursorBegin &""
Response. Write "the ID value (CursorEnd) of the last record =" & CursorEnd & "<br>"
%>
<Script language = "javascript">
Function turnpage (func ){
Var CurPageNum = <% = CurPageNum %>; // obtain the current page number
Var CursorBegin = <% = CursorBegin %>; // obtain the ID value of the first displayed record
Var CursorEnd = <% = CursorEnd %>; // obtain the ID of the last displayed record
Var TotalPages = <% = TotalPages %>; // retrieves the total number of pages
Var BackUrl = 'mllist. asp? CurPageNum = '+ (CurPageNum-1) +' & CursorBegin = '+ CursorBegin +' & CursorEnd = '+ CursorEnd +' & hav = back ';
Var NextUrl = 'mllist.
Asp? CurPageNum = '+ (CurPageNum + 1) +' & CursorBegin = '+ CursorBegin +' & CursorEnd = '+ CursorEnd +' & hav = next ';
If (CurPageNum <= 1 & func = 'back '){
Location. href = '#';
} Else if (CurPageNum> = TotalPages & func = 'Next '){
Location. href = '#';
} Else if (func = 'back '){
Location. href = BackUrl;
} Else if (func = 'Next '){
Location. href = NextUrl;
}
}
</Script>
</Body>
</Html>
Asp? CurPageNum = '+ (CurPageNum + 1) +' & CursorBegin = '+ CursorBegin +' & CursorEnd = '+ CursorEnd +' & hav = next ';
If (CurPageNum <= 1 & func = 'back '){
Location. href = '#';
} Else if (CurPageNum> = TotalPages & func = 'Next '){
Location. href = '#';
} Else if (func = 'back '){
Location. href = BackUrl;
} Else if (func = 'Next '){
Location. href = NextUrl;
}
}
</Script>
</Body>
</Html>
Cnbruce code:
Paging example: [homepage] [Previous Page] [Next Page] [last page] [page: 4/5 page] [86 articles, 20 articles/Page] Go to: _ page
The following is a public code that must be used.
<% Filepath = request. servervariables ("path_info") %>
<% Page = 1' set the initial value of the variable PAGE = 1
Page = request. querystring ("page") 'page value is the accepted value.
Rs. PageSize = 20' number of records displayed per page
If Not IsEmpty (trim (Request ("Page") then' if the PAGE has been initialized...
Page = CInt (Request ("Page") 'receives the PAGE and assigns it to the PAGE variable as a number.
If Page> rs. PageCount then', if the number of pages received exceeds the total number of pages
Rs. AbsolutePage = rs. PageCount
Elseif Page <= 0 then' if the page is smaller than or equal to 0
Page = 1' set PAGE to equal to the first Page
Else
Rs. AbsolutePage = Page. If it is greater than zero, the current Page is displayed equal to the number of pages received.
End if
End if
Page = rs. AbsolutePage %>
First Paging
<% If rs. pagecount <> 1 and rs. pagecount <> 0 then %> 'first, it is determined that the total number of pages is not 1 or 0.
<% If page> 1 then %>
<% If page <rs. pagecount then %>
[<A Href = "<% = filepath %>? Page = <%= 1%> "> homepage </a>]
[<A Href = "<% = filepath %>? Page = <% = page-1%> "> previous Page </a>]
[<A Href = "<% = filepath %>? Page = <% = page + 1%> "> next Page </a>]
[<A Href = "<% = filepath %>? Page = <% = rs. PageCount %> "> last Page </a>]
<% Else %>
[<A Href = "<% = filepath %>? Page = <%= 1%> "> homepage </a>]
[<A Href = "<% = filepath %>? Page = <% = page-1%> "> previous Page </a>]
[Next Page] [Last Page]
<% End if %>
<% Else %>
[Homepage] [Previous Page]
[<A Href = "<% = filepath %>? Page = <% = page + 1%> "> next Page </a>]
[<A Href = "<% = filepath %>? Page = <% = rs. PageCount %> "> last Page </a>]
<% End if %>
<% Else %>
[Homepage] [Previous Page] [Next Page] [Last Page]
<% End if %>
Second Paging
<% If rs. pagecount <> 1 and rs. pagecount <> 0 then %>
<% If page> 1 then %>
[<A Href = "<% = filepath %>? Page = <%= 1%> "> homepage </a>]
[<A Href = "<% = filepath %>? Page = <% = page-1%> "> previous Page </a>]
<% If page <rs. pagecount then %>
[<A Href = "<% = filepath %>? Page = <% = page + 1%> "> next Page </a>]
[<A Href = "<% = filepath %>? Page = <% = rs. PageCount %> "> last Page </a>]
<% Else %>
[Next Page] [Last Page]
<% End if %>
<% Else %>
[Homepage] [Previous Page]
[<A Href = "<% = filepath %>? Page = <% = page + 1%> "> next Page </a>]
[<A Href = "<% = filepath %>? Page = <% = rs. PageCount %> "> last Page </a>]
<% End if %>
<% Else %>
[Homepage] [Previous Page] [Next Page] [Last Page]
<% End if %>
Third
<% If rs. pagecount <> 1 and rs. pagecount <> 0 then %>
<% If page <rs. pagecount then %>
<% If page = 1 then %>
[Homepage] [Previous Page]
<% Else %>
[<A Href = "<% = filepath %>? Page = <%= 1%> "> homepage </a>]
[<A Href = "<% = filepath %>? Page = <% = page-1%> "> previous Page </a>]
<% End if %>
[<A Href = "<% = filepath %>? Page = <% = page + 1%> "> next Page </a>]
[<A Href = "<% = filepath %>? Page = <% = rs. PageCount %> "> last Page </a>]
<% Else %>
[<A Href = "<% = filepath %>? Page = <%= 1%> "> homepage </a>]
[<A Href = "<% = filepath %>? Page = <% = page-1%> "> previous Page </a>]
[Next Page] [Last Page]
<% End if %>
<% Else %>
[Homepage] [Previous Page] [Next Page] [Last Page]
<% End if %>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.