*/ --------------------------------------------------------------------------------------
*/ 出自: 建站学 http://www.jzxue.com
*/ 作者: 天涯听雨 E-mail:qcfy2005@163.com 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 "<div style='font-size:14px;color=#ff0000' align='center'>数据库连接出错,请检查数据库连接字串</div>"
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 "<script LANGUAGE='javascript'>alert('当前没有找到任何记录,请返回重新操作!');history.go(-1);</script>"
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")&"<br/>"
rs.movenext
loop
'进行关闭和释放相关资源
rs.Close
Conn.Close : Set Conn=nothing
'显示分页
if PageNo<=1 then
Response.Write "<font color='#FF0000'>[首页] [上一页]</font>"
else
Response.Write"[<a href='?PageNo=1' target='_self' title='首页'><font color='#FF0000'>首页</font></a>] "
Response.Write"[<a href='?PageNo="&(PageNo-1)&"' target='_self' title='上一页'><font color='#FF0000'>上一页</font></a>] "
end if
if PageNo>=PageCountX then
Response.Write "<font color='#FF0000'>[下一页] [尾页]</font>"
else
Response.Write"[<a href='?PageNo="&(PageNo+1)&"' target='_self' title='下一页'><font color='#FF0000'>下一页</font></a>] "
Response.Write"[<a href='?PageNo="&PageCountX&"' target='_self' title='尾页'><font color='#FF0000'>尾页</font></a>]"
end if
Response.Write"[页次 第<font color=red>"&PageNo&"</font>页/共<font color=red>" & PageCountX &"</font>页]"
Response.Write" [共<font color=red>"&FieldCount&"</font>条 <font color=red>"& PageSizeX & "</font>条/页]"
Response.Write" 转到:<select name='page' size='1' onchange=""javascript:window.location = '?PageNo='+this.options[this.selectedIndex].value;"">"
for i=1 to PageCountX
Response.Write"<option value="&i&""
if PageNo=i then Response.Write" selected"
Response.Write(">第-"&i&"-页</option>")
next
Response.Write"</selected>"
%>
(责任编辑:admin)