Paging solution for two different database tables

Source: Internet
Author: User

Program implementation function: Data paging display of two data tables

Recently I encountered a relatively rare demand, I do not know whether we have encountered, say to share with you.

Customer requirements: Two parts of data, two tables from two databases in different locations, defined as database DB1 and DB2, table Table1 and Tabel2, to display the two table data in the upper and lower parts of the page, and with the same pagination, such as the first part of the data, will be filled with Part II data

'/* code *

<%

Set objconn1=server.createobject ("ADODB. Recordset ")
Set objconn2=server.createobject ("ADODB. Recordset ")
Objconn1.open "Provider=sqloledb;data source=localhost; Uid=sa; PWD=123;DATABASE=DB1 "
Objconn2.open "Provider=sqloledb;data source=localhost; Uid=sa; PWD=123;DATABASE=DB2 "

PAGE=CLNG (Request.QueryString ("Page"))
If page= "" Then
Page=1
End If
Strsql1= "SELECT * from Table1 ORDER BY ID Desc"
Strsql2= "SELECT * from Table2 ORDER BY ID Desc"

RECORD_COUNT1=CLNG (Objconn1.execute (StrSQL1) (0)) ' Gets the number of records in the first table
RECORD_COUNT2=CLNG (Objconn2.execute (StrSQL2) (0)) ' Gets the number of records in the second table
Record_count=record_count1+record_count2 ' Gets the number of records in two tables and

If record_count>0 Then

Page_size1=5 ' default first table partial data display number per page
Page_size2=5 ' default display number of data per page for the second table part

Page_count1=cint (RECORD_COUNT1/PAGE_SIZE1)
If (record_count1 Mod page_size1) < (PAGE_SIZE1/2) Then page_count1=page_count1+1 ' Gets the number of pages in the first part of the data
Page_count2=cint (RECORD_COUNT2/PAGE_SIZE2)
If (Record_count2 Mod page_size2) < (PAGE_SIZE2/2) Then page_count2=page_count2+1 ' Gets the number of pages in the second part of the data

If Cint (page_count2) =cint (page) Then ' assumes that the second part of the data must be small, here determines whether the current page is the last page of the minute data

thepagerecordcount=record_count2-(page-1) *5 ' The last page of data in the second part shows the number of bars
Page_size1=10-cint (Thepagerecordcount) ' The first part of the data in the second part of the last page shows the number of bars '
Page_size1=cint (Thepagerecordcount) ' Gets the number of display bars in the first section on this page
ElseIf CInt (page) >cint (page_count2) Then ' This will all be the first part of the data

page_size1=10
Page_size2=0

End If

Page_count=cint (record_count/(page_size1+page_size2) ' All two types of pages together
If (Record_count Mod (page_size1+page_size2)) < ((page_size1+page_size2)/2) Then page_count=page_count+1

If Page < 1 Then
Page = 1
End If
If Page > Page_count Then
Page = Page_count
End If

Dim Pagefrom,pageto
Pagefrom=page-10
Pageto=page+10

If Pagefrom<1 Then
Pagefrom=1
End If
If Pageto>record_count Then
Pageto=page_count
End If


End If


If page<=1 Then ' first page display query
strsql1= "Select Top" &page_size1& "* Table1"
strsql1=strsql1& "ORDER BY ID Desc"
strsql2= "Select Top" &page_size1& "* Table2"
strsql2=strsql2& "ORDER BY id DESC"

Else ' Nth page display query
strsql1= "Select Top" & (Page-1) *page_size1& "IDs from Table1"
strsql1=strsql1& "ORDER by ID Desc"
strsql1= "SELECT top 1 ID" ("&StrSQL1&") derivedtbl ORDER by ID
' Response.Write (SQL)
ID =trim (Objconn1.execute (StrSQL1) (0))
strsql1= "Select Top" &page_size1& "* Table1"
strsql1=strsql1& "where id< '" &id& ""
strsql1=strsql1& "ORDER by ID Desc"


strsql2= "Select Top" & (Page-1) *page_size2& "IDs from Table2"
strsql2=strsql2& "ORDER by ID Desc"
Strsql2= "SELECT top 1 ID" ("&StrSQL2&") derivedtbl ORDER by ID
ID =trim (Objconn2.execute (StrSQL2) (0))

strsql2= "Select Top" &page_size2& "* Table2"
strsql2=strsql2& "where id< '" "&id &" "
strsql2=strsql2& "ORDER by ID Desc"

End If

%>

<%
If record_count>0 Then

%>
<%
Set rs1=server.createobject ("Adodb.recordset")
Rs1.open strsql1,objconn1
For n = 1 to Page_size1
If rs1. Eof Then Exit for
%>
<!--display of the first part of the content loop-->
<%
Rs1. MoveNext
If rs1. EOF Then Exit for
Next
Rs1.close
Set rs1=nothing
%>
<%
Set rs2=server.createobject ("Adodb.recordset")
Rs2.open strsql2,objconn2
For n = 1 to Page_size2
If rs2. Eof Then Exit for
%>
<!--display of the second part of the content loop-->
<%
Rs2. MoveNext
If rs2. EOF Then Exit for
Next
Rs2.close
Set rs2=nothing
%>

<!--results page-->
<%if page<> "1" then%><a href=?page=<%=cint (page-1)%>> prev </a> <%end if%>
<% ' writes out page connections with a For loop
For I=pagefrom to PageTo
If I=0 Then
I=1
End If

If I<>cint (page) Then
Strurl= "<a href=?page=" &i& "><font color= #000000 >" &i& "</font></a>"

Else
Strurl= "<b><font color= #ce0031 >" &i& "</font></b>"
End If

Response.Write strURL
Response.Write ""
Next
%>
<%if page_count =1 or (Page_count-cint (page)) =0 Then
Response. Write ("")
Else
Response. Write "<a href=?page=" &cint (page+1) & "> Next </a>"
End If
%>
<!--results page-->

'/* Code end */

My level is limited, the procedure is rough, may have the shortcoming ~ Hope criticize correction ~



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.