asp源代码:百万级数据分页代码
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/23 13:41:28 ASP
*/ --------------------------------------------------------------------------------------
*/ 出自: 建站学 http://www.jzxue
*/ 作者: 天涯听雨 E-mail:qcfy2005@163 QQ:809252803
*/ 时间: 2007-10-6 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------
<%
'名称:百万数据进行分页
'作者:天涯听雨
'时间:2007年10月6日
'平台:Win2000+IIS5.0+Sql2000
'附言:第一次写就运用简单一些,大家不要骂……
'----------------------------------------------------------连接数据库
Response.Buffer = True
Dim SqlLocalName,SqlUsername,SqlPassword,SqlDatabaseName
Dim ConnStr,Conn
'''''''''''''''''''''''''''''' SQL数据库 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
SqlLocalName ="(local)" '连接IP [ 本地用 (local) 外地用IP ]
SqlUsername ="sa" '数据库用户名
SqlPassword ="XXXXXX" '用户密码
SqlDatabaseName="News2008" '数据库名
ConnStr = "Provider=Sqloledb;User ID=" & SqlUsername & "; Password=" & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source=" & SqlLocalName & ";"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
Set Conn=Server.CreateObject("ADODB.Connection")
Conn.open ConnStr
If Err Then
err.Clear
Set Conn = Nothing
Response.Write "
Response.End
End If
'----------------------------------------------------------连接数据库完成
'定义变量
'TblName-表名;FldName-排列字段名;FieldName-需读取的字段名;PageSizeX-分页大小;PageCountX-总页数;PageNo-当前页;OrderType-排序方式;StrWhere-条件语句
'StrSql-总执行语句;FieldCount-总记录数;StrTmp,StrOrder-临时变量
Dim TblName,FldName,FieldName,PageSizeX,PageCountX,PageNo,OrderType,StrWhere
Dim StrSql,FieldCount,StrTmp,StrOrder,TopField
'进行赋值
PageNo=trim(Request.Querystring("PageNo"))
if isnumeric(PageNo) or PageNo="" then '如果没有Page值,进行初始化值
PageNo=abs(PageNo)
if PageNo=0 then PageNo=1
else
PageNo=1
end if
TblName = "TableNews"
FldName = "ID" '说明,排序字段值在数据库不能有重复 : 建议是主键
FieldName="ID,Title,AddTime"
PageSizeX=30
OrderType=1 '0-为降序 1-升序
StrWhere=" (Code='0101') and (Hits>100) " '注意: 一个条件就用()双括号括起来 第一个不用and
TopField=(PageNo-1)*PageSizeX 'TOP多少条
if OrderType=0 then
StrTmp = "<(Select min"
StrOrder = " Order BY ["&FldName&"] desc"
else
StrTmp = ">(Select max"
StrOrder = " Order BY ["&FldName&"] asc"
end if
'定义sql语句
StrSql="Select Top "&Clng(PageSizeX)&" "& FieldName &" From "&TblName&" "
if PageNo=1 then '如果是第一页时,执行如下代码查库速度快
if StrWhere<>"" then
StrSql=StrSql&" Where "&StrWhere & StrOrder&""
else
StrSql=StrSql & StrOrder
end if
else
if StrWhere<>"" then
StrSql=StrSql&" Where "& FldName & StrTmp&"("&FldName&") From (Select top "& Clng(TopField)&" "&FldName &" From "&TblName&" Where "& StrWhere & StrOrder &") as tblTmp) and "& StrWhere & StrOrder&""
else
StrSql=StrSql&" Where "& FldName & StrTmp&"("&FldName&") From (Select top "& Clng(TopField)&" "&FldName &" From "&TblName & StrOrder &") as tblTmp) "&StrOrder&""
end if
end if
Set rs=Conn.exeCute(StrSql) '执行查询
if rs.bof and rs.eof then
Conn.Close : set Conn=nothing
response.write ""
response.end
end if
if StrWhere<>"" then '求总记录数
FieldCount=Conn.exeCute("Select Count("&FldName&") From "&TblName&" Where "&StrWhere&"")(0)
else
FieldCount=Conn.exeCute("Select Count("&FldName&") From "&TblName&"")(0)
end if
if (FieldCount mod nPageSize)>0 then '求总页数 PageCountX
PageCountX=((FieldCount - (FieldCount mod PageSizeX))/ PageSizeX)+1
else
PageCountX= (FieldCount / PageSizeX)
end if
'显示数据
do while not rs.eof
response.write""&rs("Title")&" 添加时间:"&rs("AddTime")&"
"
rs.movenext
loop
'进行关闭和释放相关资源
rs.Close
Conn.Close : Set Conn=nothing
'显示分页
if PageNo<=1 then
Response.Write "[首页] [上一页]"
else
Response.Write"[首页] "
Response.Write"[上一页] "
end if
if PageNo>=PageCountX then
Response.Write "[下一页] [尾页]"
else
Response.Write"[下一页] "
Response.Write"[尾页]"
end if
Response.Write"[页次 第"&PageNo&"页/共" & PageCountX &"页]"
Response.Write" [共"&FieldCount&"条 "& PageSizeX & "条/页]"
Response.Write" 转到:
for i=1 to PageCountX
Response.Write"")
next
Response.Write""
%>
ASP