Asp+sqlserver paging method (without stored procedures)

Source: Internet
Author: User
Tags count sort
One of my ASP + SQL Server paging programs: The feature of this program is to drop the current page record on the page. Good performance, and do not use stored procedures, because the code is not encapsulated, so very fragmented, to use your program, you need to read the first simple. Then pay attention to the meaning of the place you need to set yourself. Some of them can be omitted. Some of the parameter passes can also be omitted.

The code is as follows:

"' The snow in the northern part of the page (SQL Server) without stored procedures-------------------------

' There are # # in the comments #的需要用户设置
' A description of the parameter passing * in the annotation is passed by parameter.


' Define variables
Dim toption ' Query Criteria
Dim Torder ' Sort String
Dim Torderfield ' Sort field can be obtained by parameter: Order_field
Dim torderdirection ' sort direction can be obtained by parameter: order_direction

Dim tpagesize                ' Page Size
Dim ttotalcount              ' Total Record number          available by parameter: T_count
Dim tpagecount                ' Pages
Dim tcurpage                  ' current page number         can be obtained by parameter: page

Dim ttablename ' Table or view name
Alias for Dim Tfieldalias ' line Number
Dim tfieldlist ' Field List for query
Dim Tpagefield ' fields for paging

Dim R_count ' Number of records checked


Set Rs=server.createobject ("Adodb.recordset") ' Recordset object

' Sort processing
Torderfield=request ("Order_field") ' Get sort field (parameter pass * *)
Torderdirection=request ("Order_dir") ' Get sort direction (parameter pass * *)

if (torderfield= "") then torderfield= "Item_code" ### set the default sort field
if (torderdirection= "") then torderdirection= "ASC" ### set the default sort direction
' Www.knowsky.com
Torder= "ORDER BY" & Torderfield & "& torderdirection &" "' Generate sort string


' Define Parameters
Tpagesize=find_rs_count ' ### set page size
Ttablename= "View_select1" ' ### set up a table or view with a query
tfieldlist= "*" ' ### list of fields to query
Tpagefield= "Item_code" ### set a field for a primary key or unique index for paging calculations


' Page handling
Tcurpage=request ("page") ' Get current page (parameter pass * *)
Ttotalcount=request ("T_count") ' Get total number of pages (parameter pass * *)

if (tcurpage= "") then tcurpage=1
if (CInt (tcurpage) =0) then tcurpage=1
if (tpagecount= "") then Tpagecount =1
if (CInt (tpagecount) =0) then Tpagecount=1

' Constructs the query condition, according to the concrete procedure, certainly is different. But the final condition must be "where??? ”
toption= "issue_flag= ' Y" "' ### set condition
If f_c<> "" Then toption= toption & F_c ' ### set conditions

If trim (toption) = "" Then
Toption = "where 1=1" ' If there is no condition, add one by yourself.
Else
Toption= "where" & Toption
End If



' Constructs the query string, the core of this paging program, which is the record we need to download only the current page
if (tcurpage>1) then
Constr= ' select Top ' & tpagesize & ' & tfieldlist & ' from ' & Ttablename & Toption
Constr =constr & "and" & Tpagefield & "Not in (select Top" & tpagesize* (tCurPage-1) & "" & TPa Gefield & "from" & Ttablename & Toption & "" & Torder & ")" & Torder
Else
Constr= ' select Top ' & tpagesize & ' & tfieldlist & ' from ' & Ttablename & toption & ' "&A mp Torder
End If


' Execute the main query and get the corresponding recordset
Call Conndatabase () ' ### Establish a database connection
Rs.cursorlocation=3
Rs.Open constr,conn,3,1 ' Execute query
R_count= Rs.recordcount


' When the total number of records has not been queried and the total record count exceeds the page size, the total number of records in the current condition is queried
if (r_count>=tpagesize or tcurpage>1) and ttotalcount=0 Then
Set Rr=conn.execute ("SELECT count (*) from" & Ttablename & "" & Toption)
TTOTALCOUNT=RR (0)
Rr.close ()
Set rr=nothing
End If
if (CInt (ttotalcount) =0) then Ttotalcount=r_count ' If the total record is 0, set the record number of the currently poor recordset to the total number of records, indicating that the total number of records currently is less than the page size

' Calculate pages using page size and total record count
if (CInt (ttotalcount) >cint (tpagesize)) Then
Tpagecount=cint (CInt (ttotalcount) \ CInt (tpagesize))
if (CInt (ttotalcount) mod CInt (tpagesize)) >0 Then
Tpagecount =tpagecount +1
End If
End If

Tcurpage=cint (Tcurpage)
Tpagecount=cint (Tpagecount)


'  ---------------------------------------------------------------------

This is the whole code, interested friends, you can study, or encapsulation of him, plus pager and other methods. In short, I hope this code will be useful to everyone.



Related Article

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.