最简单使用方法(适用于任何数据表):
test.asp
test.asp
<!--
#include file="conn.asp"
-->
<!-- #include file="Page.asp" -->
<%
Set My = New Page
With My
.SetConnstr = Connstr ' 数据库链接字符串
.SetStrTable = " users " ' 表名
.SetStrText = " id,username,type,names " ' 查询的字段
.SetStrIndex = " id " ' 主键
rsArray = .GetRs() ' 返回数据,类型为数组
End With
%>
< table >
<%
if IsArray (rsArray) then
For i = 0 To UBound (rsArray, 2 )
%>
< tr >
< td > <% = rsArray( 0 , i) %> </ td >
< td > <% = rsArray( 1 , i) %> </ td >
< td > <% = rsArray( 2 , i) %> </ td >
</ tr >
<%
next
end if
%>
< tr >
< td colspan ="4" > 共 <% = My.GetTotalNum %> 条 每页 <% = My.GetiPageSize %> 条 共 <% = My.GetTotalPage %> 页 页码: <% = My.GetFenYeJmp( false ) %> </ td >
</ tr >
<!-- #include file="Page.asp" -->
<%
Set My = New Page
With My
.SetConnstr = Connstr ' 数据库链接字符串
.SetStrTable = " users " ' 表名
.SetStrText = " id,username,type,names " ' 查询的字段
.SetStrIndex = " id " ' 主键
rsArray = .GetRs() ' 返回数据,类型为数组
End With
%>
< table >
<%
if IsArray (rsArray) then
For i = 0 To UBound (rsArray, 2 )
%>
< tr >
< td > <% = rsArray( 0 , i) %> </ td >
< td > <% = rsArray( 1 , i) %> </ td >
< td > <% = rsArray( 2 , i) %> </ td >
</ tr >
<%
next
end if
%>
< tr >
< td colspan ="4" > 共 <% = My.GetTotalNum %> 条 每页 <% = My.GetiPageSize %> 条 共 <% = My.GetTotalPage %> 页 页码: <% = My.GetFenYeJmp( false ) %> </ td >
</ tr >
</
table
>
还有很多功能可以拓展,复杂的查询、排序等,不一一演示了这个是使用到的asp类->Page.asp
'*******************************************************
'Page 分页类
'特点:采用 not in 分页输出方法,需配合 sp_Page 存储过程使用,速度比较快
'输入参数 connstr:数据库链接字符串 StrTable:要查询的表 StrText:要查询的字段 StrIndex:索引字段
'可选参数:(iPageSize:每页记录数量 StrWhere:条件语句 StrOrder:排序字段 StrSc:排序方法 ActionStr:From提交的查询参数)
'输出参数 rsArray:返回的记录集数组 GetTotalNum:返回的总记录数 GetiPage:当前页码 GetTotalPage:总页数 GetiPageSize:每页显示条数
'输出方法 GetFenYe():分页 GetFenYeJmp(inForm):带跳转的分页
'作者:六月雨 QQ:44569457 整理时间:2008年9月27日
'*******************************************************
Class Page
Private connstr '数据库链接字符串
Private iPage '当前页码
Private iPageSize '每页记录数量
Private StrTable '要查询的表
Private StrText '要查询的字段
Private StrWhere '条件语句
Private StrIndex '索引字段
Private StrOrder '排序字段
Private StrSc '排序方法
Private ActionStr '翻页链接字符
Private Rs_dbs '记录集名称
Private cmd 'cmd对象名称
Private rsArray '返回的记录集数组
Private TotalNum '返回的总记录数
Private Sub Class_Initialize() '初始化类
iPageSize=10
iPage=trim(Request("iPage"))
ActionStr=""
StrWhere=""
StrOrder=""
StrSc=""
If (not IsNumeric(iPage)) Then
iPage=1
Else
If iPage < 1 then iPage =1
If iPage > 5000000 then iPage=1
End If
End Sub
Private Sub Class_Terminate() '释放类
set Rs_dbs=nothing
End Sub
Public Property Let SetConnstr(svalue) '取得:数据库链接字符串
connstr=Lcase(svalue)
End Property
Public Property Let SetiPageSize(svalue) '取得:每页记录数量
iPageSize=clng(svalue)
End Property
Public Property Let SetStrTable(svalue) '取得:SQL表名
StrTable=Lcase(svalue)
End Property
Public Property Let SetStrText(svalue) '取得:SQL查询的字段
StrText=Lcase(svalue)
End Property
Public Property Let SetStrWhere(svalue) '取得:SQL条件字段
StrWhere=Lcase(svalue)
End Property
Public Property Let SetStrIndex(svalue) '取得:索引字段
StrIndex=Lcase(svalue)
End Property
Public Property Let SetStrOrder(svalue) '取得:排序字段
StrOrder=Lcase(svalue)
End Property
Public Property Let SetStrSc(svalue) '取得:排序方法
StrSc=Lcase(svalue)
End Property
Public Property Let SetActionStr(svalue) '取得:翻页链接字符
ActionStr=Lcase(svalue)
End Property
Private Function MadeOrderBy() '方法:组合Order By 语句
dim TempStrOrder,TempStrSc,t
if StrOrder <> "" and StrSc <> "" then
TempStrOrder=split(StrOrder,",")
TempStrSc=split(StrSc,",")
if ubound(TempStrOrder)=ubound(TempStrSc) then
for t=0 to ubound(TempStrOrder)
if t=0 then
MadeOrderBy=TempStrOrder(t)&" " &TempStrSc (t)
else
MadeOrderBy=MadeOrderBy&"," &TempStrOrder (t)&" " &TempStrSc (t)
end if
next
end if
end if
End Function
Private Sub OpenRs() '方法:获得记录集
Set Rs_dbs=Server.CreateObject("ADODB.RECORDSET")
Set cmd = Server.CreateObject("ADODB.Command")
with cmd
.ActiveConnection = connstr '数据库连接字串
.CommandText = "sp_Page" '指定存储过程名
.CommandType = 4 '表明这是一个存储过程
.Prepared = true '要求将SQL命令先行编译
.Parameters.append .CreateParameter("@iPage",3,1,4,iPage) '指定页数
.Parameters.append .CreateParameter("@iPageSize",3,1,4,iPageSize) '每页记录数
.Parameters.append .CreateParameter("@StrTable",200,1,200,StrTable) '分页时要查询的表名
.Parameters.append .CreateParameter("@StrText",200,1,1000,StrText) '字段
.Parameters.append .CreateParameter("@StrWhere",200,1,1000,StrWhere) '查询条件where 中的条件语句
.Parameters.append .CreateParameter("@StrIndex",200,1,30,StrIndex) '索引值
.Parameters.append .CreateParameter("@StrOrder",200,1,100,MadeOrderBy()) '排序的字段
.Parameters.Append .CreateParameter("@StrTotals",3,2,10) '总页数output
Set Rs_dbs = .Execute
end with
End Sub
Public Property Get GetRs() '输出:记录集(数组方式)
call OpenRs()
If not (Rs_dbs.eof and Rs_dbs.bof ) then
GetRs = Rs_dbs.GetRows()
End If
Rs_dbs.close
TotalNum = cmd(7)
set cmd = nothing
End Property
Public Property Get GetTotalNum '输出:总记录数
GetTotalNum=TotalNum
End Property
Public Property Get GetTotalPage '输出:总页数
If (TotalNum mod iPageSize) <> 0 Then
GetTotalPage=(TotalNum\iPageSize)+1
Else
GetTotalPage=(TotalNum\iPageSize)
End If
End Property
Public Property Get GetiPageSize '输出:每页显示条数
GetiPageSize=iPageSize
End Property
Public Property Get GetiPage '输出:当前页码
GetiPage=iPage
End Property
Private Function GetQueryUrl() '方法:获得当前URL
dim UrlFile,Query,Querys,i
UrlFile= Request.Servervariables("URL")
Query= Request.Servervariables("QUERY_STRING")
Querys=split(Query,"&")
For i=0 to ubound(Querys)
if trim(Querys(i)) <> "" then
If trim(Querys(i))=replace(trim(Querys(i)),"iPage=","") then
GetQueryUrl=GetQueryUrl &Querys (i)&"&"
End If
End If
Next
if ActionStr="" then
GetQueryUrl=UrlFile&"?" &GetQueryUrl
else
GetQueryUrl=UrlFile&"?" &ActionStr &"&" &GetQueryUrl
end if
End Function
Public Property Get GetFenYe() '输出:分页
If (iPage>=1) and (clng(iPage) < =clng (GetTotalPage)) then
dim x,y,m,n,i,ActionUrL
ActionUrL =GetQueryUrl()
If iPage ="" then iPage =1
iPage =Clng(iPage)
If TotalNum< > 0 then
x=TotalNum\iPageSize
y=TotalNum mod iPageSize
If y <> 0 then x=x+1 '总页数
if iPage>5 then
If (iPage+5) < =x then
n =iPage-4
m =iPage+5
Else
n =iPage-4
m =x
End If
Else
n =1
m =10
End If
If x< =10 then
If x< > 1 then
if iPage <> 1 then
Response.Write" < a href ="&ActionUrL&" iPage ="&(iPage-1)&" > 上一页 </ a > "
end if
for i=1 to x
if iPage=i then
Response.Write(i)
Else
Response.Write" < a href ="&ActionUrL&" iPage ="&i&" > [" &i &"] </ a > "
End If
next
if iPage <> x then
Response.Write" < a href ="&ActionUrL&" iPage ="&(iPage+1)&" > 下一页 </ a > "
end if
End If
Else
If iPage>5 then
Response.Write" < a href ="&ActionUrL&" iPage =1 > [首页] </ a > "
End If
If iPage <> 1 then
Response.Write" < a href ="&ActionUrL&" iPage ="&(iPage-1)&" > 上一页 </ a > "
end if
for i=n to m
if iPage>x then Exit For
if iPage=i then
Response.Write(i)
Else
Response.Write" < a href ="&ActionUrL&" iPage ="&i&" > [" &i &"] </ a > "
End If
next
if iPage <> x then
Response.Write" < a href ="&ActionUrL&" iPage ="&(iPage+1)&" > 下一页 </ a > "
End If
if (iPage+5) < x then
Response.Write" <a href ="&ActionUrL&" iPage ="&x&" > [尾页] </ a > "
End If
End If
End If
End If
End Property
Public Property Get GetFenYeJmp(inForm) '输出:带跳转的分页
if inForm then
response.Write(GetFenYe()&" < input name ='iPage' type ='text' value ="&iPage&" id ='iPage' size ='3' > ")
response.Write(" < input type ='submit' name ='Submit' value ='转' > ")
else
response.Write(" < form name ='Jmp' method ='post' action ="&GetQueryUrl()&" > ")
response.Write(GetFenYe()&" < input name ='iPage' type ='text' value ="&iPage&" id ='iPage' size ='3' > ")
response.Write(" < input type ='submit' name ='Submit' value ='转' > ")
response.Write(" </ form > ")
end if
End Property
End Class
'Page 分页类
'特点:采用 not in 分页输出方法,需配合 sp_Page 存储过程使用,速度比较快
'输入参数 connstr:数据库链接字符串 StrTable:要查询的表 StrText:要查询的字段 StrIndex:索引字段
'可选参数:(iPageSize:每页记录数量 StrWhere:条件语句 StrOrder:排序字段 StrSc:排序方法 ActionStr:From提交的查询参数)
'输出参数 rsArray:返回的记录集数组 GetTotalNum:返回的总记录数 GetiPage:当前页码 GetTotalPage:总页数 GetiPageSize:每页显示条数
'输出方法 GetFenYe():分页 GetFenYeJmp(inForm):带跳转的分页
'作者:六月雨 QQ:44569457 整理时间:2008年9月27日
'*******************************************************
Class Page
Private connstr '数据库链接字符串
Private iPage '当前页码
Private iPageSize '每页记录数量
Private StrTable '要查询的表
Private StrText '要查询的字段
Private StrWhere '条件语句
Private StrIndex '索引字段
Private StrOrder '排序字段
Private StrSc '排序方法
Private ActionStr '翻页链接字符
Private Rs_dbs '记录集名称
Private cmd 'cmd对象名称
Private rsArray '返回的记录集数组
Private TotalNum '返回的总记录数
Private Sub Class_Initialize() '初始化类
iPageSize=10
iPage=trim(Request("iPage"))
ActionStr=""
StrWhere=""
StrOrder=""
StrSc=""
If (not IsNumeric(iPage)) Then
iPage=1
Else
If iPage < 1 then iPage =1
If iPage > 5000000 then iPage=1
End If
End Sub
Private Sub Class_Terminate() '释放类
set Rs_dbs=nothing
End Sub
Public Property Let SetConnstr(svalue) '取得:数据库链接字符串
connstr=Lcase(svalue)
End Property
Public Property Let SetiPageSize(svalue) '取得:每页记录数量
iPageSize=clng(svalue)
End Property
Public Property Let SetStrTable(svalue) '取得:SQL表名
StrTable=Lcase(svalue)
End Property
Public Property Let SetStrText(svalue) '取得:SQL查询的字段
StrText=Lcase(svalue)
End Property
Public Property Let SetStrWhere(svalue) '取得:SQL条件字段
StrWhere=Lcase(svalue)
End Property
Public Property Let SetStrIndex(svalue) '取得:索引字段
StrIndex=Lcase(svalue)
End Property
Public Property Let SetStrOrder(svalue) '取得:排序字段
StrOrder=Lcase(svalue)
End Property
Public Property Let SetStrSc(svalue) '取得:排序方法
StrSc=Lcase(svalue)
End Property
Public Property Let SetActionStr(svalue) '取得:翻页链接字符
ActionStr=Lcase(svalue)
End Property
Private Function MadeOrderBy() '方法:组合Order By 语句
dim TempStrOrder,TempStrSc,t
if StrOrder <> "" and StrSc <> "" then
TempStrOrder=split(StrOrder,",")
TempStrSc=split(StrSc,",")
if ubound(TempStrOrder)=ubound(TempStrSc) then
for t=0 to ubound(TempStrOrder)
if t=0 then
MadeOrderBy=TempStrOrder(t)&" " &TempStrSc (t)
else
MadeOrderBy=MadeOrderBy&"," &TempStrOrder (t)&" " &TempStrSc (t)
end if
next
end if
end if
End Function
Private Sub OpenRs() '方法:获得记录集
Set Rs_dbs=Server.CreateObject("ADODB.RECORDSET")
Set cmd = Server.CreateObject("ADODB.Command")
with cmd
.ActiveConnection = connstr '数据库连接字串
.CommandText = "sp_Page" '指定存储过程名
.CommandType = 4 '表明这是一个存储过程
.Prepared = true '要求将SQL命令先行编译
.Parameters.append .CreateParameter("@iPage",3,1,4,iPage) '指定页数
.Parameters.append .CreateParameter("@iPageSize",3,1,4,iPageSize) '每页记录数
.Parameters.append .CreateParameter("@StrTable",200,1,200,StrTable) '分页时要查询的表名
.Parameters.append .CreateParameter("@StrText",200,1,1000,StrText) '字段
.Parameters.append .CreateParameter("@StrWhere",200,1,1000,StrWhere) '查询条件where 中的条件语句
.Parameters.append .CreateParameter("@StrIndex",200,1,30,StrIndex) '索引值
.Parameters.append .CreateParameter("@StrOrder",200,1,100,MadeOrderBy()) '排序的字段
.Parameters.Append .CreateParameter("@StrTotals",3,2,10) '总页数output
Set Rs_dbs = .Execute
end with
End Sub
Public Property Get GetRs() '输出:记录集(数组方式)
call OpenRs()
If not (Rs_dbs.eof and Rs_dbs.bof ) then
GetRs = Rs_dbs.GetRows()
End If
Rs_dbs.close
TotalNum = cmd(7)
set cmd = nothing
End Property
Public Property Get GetTotalNum '输出:总记录数
GetTotalNum=TotalNum
End Property
Public Property Get GetTotalPage '输出:总页数
If (TotalNum mod iPageSize) <> 0 Then
GetTotalPage=(TotalNum\iPageSize)+1
Else
GetTotalPage=(TotalNum\iPageSize)
End If
End Property
Public Property Get GetiPageSize '输出:每页显示条数
GetiPageSize=iPageSize
End Property
Public Property Get GetiPage '输出:当前页码
GetiPage=iPage
End Property
Private Function GetQueryUrl() '方法:获得当前URL
dim UrlFile,Query,Querys,i
UrlFile= Request.Servervariables("URL")
Query= Request.Servervariables("QUERY_STRING")
Querys=split(Query,"&")
For i=0 to ubound(Querys)
if trim(Querys(i)) <> "" then
If trim(Querys(i))=replace(trim(Querys(i)),"iPage=","") then
GetQueryUrl=GetQueryUrl &Querys (i)&"&"
End If
End If
Next
if ActionStr="" then
GetQueryUrl=UrlFile&"?" &GetQueryUrl
else
GetQueryUrl=UrlFile&"?" &ActionStr &"&" &GetQueryUrl
end if
End Function
Public Property Get GetFenYe() '输出:分页
If (iPage>=1) and (clng(iPage) < =clng (GetTotalPage)) then
dim x,y,m,n,i,ActionUrL
ActionUrL =GetQueryUrl()
If iPage ="" then iPage =1
iPage =Clng(iPage)
If TotalNum< > 0 then
x=TotalNum\iPageSize
y=TotalNum mod iPageSize
If y <> 0 then x=x+1 '总页数
if iPage>5 then
If (iPage+5) < =x then
n =iPage-4
m =iPage+5
Else
n =iPage-4
m =x
End If
Else
n =1
m =10
End If
If x< =10 then
If x< > 1 then
if iPage <> 1 then
Response.Write" < a href ="&ActionUrL&" iPage ="&(iPage-1)&" > 上一页 </ a > "
end if
for i=1 to x
if iPage=i then
Response.Write(i)
Else
Response.Write" < a href ="&ActionUrL&" iPage ="&i&" > [" &i &"] </ a > "
End If
next
if iPage <> x then
Response.Write" < a href ="&ActionUrL&" iPage ="&(iPage+1)&" > 下一页 </ a > "
end if
End If
Else
If iPage>5 then
Response.Write" < a href ="&ActionUrL&" iPage =1 > [首页] </ a > "
End If
If iPage <> 1 then
Response.Write" < a href ="&ActionUrL&" iPage ="&(iPage-1)&" > 上一页 </ a > "
end if
for i=n to m
if iPage>x then Exit For
if iPage=i then
Response.Write(i)
Else
Response.Write" < a href ="&ActionUrL&" iPage ="&i&" > [" &i &"] </ a > "
End If
next
if iPage <> x then
Response.Write" < a href ="&ActionUrL&" iPage ="&(iPage+1)&" > 下一页 </ a > "
End If
if (iPage+5) < x then
Response.Write" <a href ="&ActionUrL&" iPage ="&x&" > [尾页] </ a > "
End If
End If
End If
End If
End Property
Public Property Get GetFenYeJmp(inForm) '输出:带跳转的分页
if inForm then
response.Write(GetFenYe()&" < input name ='iPage' type ='text' value ="&iPage&" id ='iPage' size ='3' > ")
response.Write(" < input type ='submit' name ='Submit' value ='转' > ")
else
response.Write(" < form name ='Jmp' method ='post' action ="&GetQueryUrl()&" > ")
response.Write(GetFenYe()&" < input name ='iPage' type ='text' value ="&iPage&" id ='iPage' size ='3' > ")
response.Write(" < input type ='submit' name ='Submit' value ='转' > ")
response.Write(" </ form > ")
end if
End Property
End Class
存储过程
CREATE
PROCEDURE
[
dbo
]
.
[
sp_Page
]
@iPage int = 1 , -- 当前页码
@iPageSize int = 10 , -- 每页条数
@StrTable varchar ( 200 ), -- 查询的表
@StrText varchar ( 1000 ), -- 查询的字段
@StrWhere varchar ( 1000 ), -- 条件
@StrIndex varchar ( 30 ), -- 索引
@StrOrder varchar ( 100 ) = '' , -- 排序字段
@StrTotals int output -- 返回总条数
AS
-- 定义变量
declare @SqlCount nvarchar ( 2000 )
declare @Sql nvarchar ( 2000 )
declare @TempOrder nvarchar ( 1000 )
if @StrOrder <> ""
begin
set @TempOrder = ' order by ' + @StrOrder
end
else
begin
set @TempOrder = ''
end
-- 组合sql语句
if @iPage = 1
begin
set @Sql = ' select top ' + str ( @iPageSize ) + ' ' + @StrText + ' from ' + @StrTable + ' where 1=1 ' + @StrWhere + @TempOrder
end
else
begin
set @Sql = ' select top ' + str ( @iPageSize ) + ' ' + @StrText + ' from ' + @StrTable + ' where ' + @StrIndex + ' not in (select top ' + str ( @iPageSize * ( @iPage - 1 )) + ' ' + @StrIndex + ' from ' + @StrTable + ' where 1=1 ' + @StrWhere + @TempOrder + ' ) ' + @StrWhere + @TempOrder
end
set @SqlCount = ' select @StrTotals=isnull(count(*),10000) from ' + @StrTable + ' where 1=1 ' + @StrWhere
-- 查询总记录数量
exec sp_executesql @SqlCount ,N ' @StrTotals int output ' , @StrTotals output
-- 执行sql语句返回
exec ( @Sql )
GO
@iPage int = 1 , -- 当前页码
@iPageSize int = 10 , -- 每页条数
@StrTable varchar ( 200 ), -- 查询的表
@StrText varchar ( 1000 ), -- 查询的字段
@StrWhere varchar ( 1000 ), -- 条件
@StrIndex varchar ( 30 ), -- 索引
@StrOrder varchar ( 100 ) = '' , -- 排序字段
@StrTotals int output -- 返回总条数
AS
-- 定义变量
declare @SqlCount nvarchar ( 2000 )
declare @Sql nvarchar ( 2000 )
declare @TempOrder nvarchar ( 1000 )
if @StrOrder <> ""
begin
set @TempOrder = ' order by ' + @StrOrder
end
else
begin
set @TempOrder = ''
end
-- 组合sql语句
if @iPage = 1
begin
set @Sql = ' select top ' + str ( @iPageSize ) + ' ' + @StrText + ' from ' + @StrTable + ' where 1=1 ' + @StrWhere + @TempOrder
end
else
begin
set @Sql = ' select top ' + str ( @iPageSize ) + ' ' + @StrText + ' from ' + @StrTable + ' where ' + @StrIndex + ' not in (select top ' + str ( @iPageSize * ( @iPage - 1 )) + ' ' + @StrIndex + ' from ' + @StrTable + ' where 1=1 ' + @StrWhere + @TempOrder + ' ) ' + @StrWhere + @TempOrder
end
set @SqlCount = ' select @StrTotals=isnull(count(*),10000) from ' + @StrTable + ' where 1=1 ' + @StrWhere
-- 查询总记录数量
exec sp_executesql @SqlCount ,N ' @StrTotals int output ' , @StrTotals output
-- 执行sql语句返回
exec ( @Sql )
GO