实例说明:
公司有新旧两部分产品分别以不同形式存在两个数据库中,由于两数据表的结构上有较大差异,不便于两数据库(表)的合并。
本例在不改动数据库(表)的情况下,实现在同一页面的无缝输出。
代码如下,没做太多的注释
file name simple.asp
<%
dim Page
dim NeedPage '存放需求页码数
dim NeedRecord '存放需求记录数
dim NextPage '翻页标志变量
dim PageSize
NextPage=False
PageSize=15
%>
<!--html代码区-->
<%
page=request.querystring("page")
if page="" then page=1
if not(isnumeric(page)) then page=1
if page<1 then page=1
page=int(page)
%>
<!--html代码区-->
<%
Call GetData(Page)
Call FillData(NeedPage,NeedRecord)
%>
<!--html代码区-->
<div align="center">
<%if page-1>0 then%><a href="simple.asp?page=<%=page-1%>"><<</a><%else%><<<%end if%>
<%if NextPage then%><a href="simple.asp?page=<%=page+1%>">>></a><%else%>>><%end if%>
</div>
<!--html代码区-->
<%
Function GetData(Page)
Dim LPRCount '存放数据集分页后最后一页的记录数
Dim i '循环记数器
'从MAD.mdb中获取数据集
Set conn1=Server.CreateObject("adodb.connection")
Set rs1=Server.CreateObject("adodb.recordset")
conn1.open "provider=microsoft.jet.oledb.4.0;data source="&server.mappath("DData1.mdb")
sql1="select * from DTable1"
rs1.open sql1,conn1,1,1
If rs1.EOF or rs1.BOF Then
NeedPage=1
NeedRecord=PageSize
Else
rs1.PageSize=PageSize
'记录集页码数大于需求页码
If rs1.PageCount>Page Then
rs1.AbsolutePage=Page
i=0
Do While Not rs1.EOF And (i<PageSize)
Call DataOutput("Detail1",rs1("id"),rs1("Field1"),rs1("Field2"),rs1("Field3"))
rs1.MoveNext
i=i+1
Loop
NeedPage=Page-rs1.PageCount
NeedRecord=0
End If
'记录集页码数等于需求页码
If rs1.PageCount=Page Then
rs1.AbsolutePage=Page
i=0
Do While Not rs1.EOF And (i<PageSize)
Call DataOutput("Detail1",rs1("id"),rs1("Field1"),rs1("Field2"),rs1("Field3"))
rs1.MoveNext
i=i+1
Loop
LPRCount=i
NeedPage=0
NeedRecord=PageSize-LPRCount
End If
||||||'记录集页码数小于需求页码
If rs1.PageCount<Page Then
LPRCount=rs1.RecordCount-(rs1.PageCount-1)*PageSize
NeedPage=Page-rs1.PageCount
NeedRecord=PageSize-LPRCount
End If
End If
'关闭记录集
rs1.close
conn1.close
Set rs1=nothing
Set conn1=nothing
End Function
Function FillData(NeedPage,NeedRecord)
'填补页面剩余记录行,并设置下翻页标记变量
Dim i '循环记数器
Dim MoveStep '记录集移动步长
'需求页码小于零(不需要填补)
If NeedPage<0 Then
NextPage=True '激活下翻页标记
Else
'从new.mdb中获取记录集
Set conn1=Server.CreateObject("adodb.connection")
Set rs1=Server.CreateObject("adodb.recordset")
conn1.open "provider=microsoft.jet.oledb.4.0;data source="&server.mappath("DData2.mdb")
sql1="select * from DTable2"
rs1.open sql1,conn1,1,1
If rs1.EOF Then
NextPage=False '关闭下翻页标记
Else
'需求页码为零(仅当需求记录数不为零时,填补页面剩余记录行)
If NeedPage=0 Then
If NeedRecord=0 Then '不需填补页面剩余记录行
NextPage=True '激活下翻页标记
Else
'填补页面剩余记录行
i=0
Do While Not rs1.EOF And (i<NeedRecord)
Call DataOutput("Detail2",rs1("id"),rs1("Field1"),rs1("Field2"),rs1("Field3"))
rs1.MoveNext
i=i+1
Loop
If rs1.RecordCount>NeedRecord Then
NextPage=True '激活下翻页标记
Else
NextPage=False '关闭下翻页标记
End If
End If
End If
'需求页码大于零
If NeedPage>0 Then
MoveStep=(NeedPage-1)*PageSize+NeedRecord '计算记录集移动步长
If rs1.RecordCount<=MoveStep Then '记录集不够
'出错处理
Else
rs1.Move MoveStep
i=0
Do While Not rs1.EOF And (i<PageSize)
Call DataOutput("Detail2",rs1("id"),rs1("Field1"),rs1("Field2"),rs1("Field3"))
rs1.MoveNext
i=i+1
Loop
If rs1.RecordCount>(MoveStep+PageSize) Then
NextPage=True '激活下翻页标记
Else
NextPage=False '关闭下翻页标记
End If
End If
End If
End If
'关闭记录集
rs1.close
conn1.close
Set rs1=nothing
Set conn1=nothing
End If
End Function
Function DataOutput(url,id,field1,field2,field3)
'生成页面记录行
Response.Write "<table width=100% border=0 cellpadding=0 cellspacing=1>"
Response.Write " <tr bgcolor='#FFFFFF'>"
Response.Write " <td width=43% align=left><b><a href="&url&".asp?product_id="&id&">"&field1&"</a></b></td>"
Response.Write " <td width=28% align=left><font color='#D03430'><b>"&field2&"</b></font></td>"
Response.Write " <td width=29% align=left>"&field3&"</td>"
Response.Write " </tr>"
Response.Write " <tr bgcolor='#FFFFFF'>"
Response.Write " <td height=1 colspan=4 background='../images/dotline.gif'></td>"
Response.Write " </tr>"
Response.Write "</table>"
End Function
%>