不连接数据库进行查询分页
查了一下网上的资料,突然想到是否可以把所需数据提到一个数组里,然后对数组进行分页呢?首先把set rs=conn.execute(sql)查询出来的结果通过rs.getrows()取出来附给一个数组,于是我就在各网站上找分页的帖子,虽然发现不少高效率分页的帖子(包括存储过程等),可结果发现全都是需要通过SQL执行的,即翻页的时候也需要执行SQL语句,此时头都晕了,优化的也只是SQL语句,此时实在没办法,只好自己努力了!终于完成了一个数组分页的粗稿,代码不是很完善,让大家一起来研究一下!代码如下:
首先有一个index.asp查询页:
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<FORM id="SearchForm" name="SearchForm" method="post" action="search.asp?act=cha">
<div class=input>
<input id="keyword" onmouseover="this.focus()" title="快速搜索您的留言记录" onfocus="this.select()" class="in" maxlength="35" name="keyword" />
<select style="width:120px;margin-top:-25px" name="ChannelID">
<option value="k2">留言人</option>
<option value="k1">留言内容</option>
</select>
<input id="search_btn" type="submit" value="查询">
</div>
</FORM>
</body>
</html>
search.asp的代码:
[code]<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<%if request.querystring("act")="cha" then
search_type=request.form("ChannelID")
keywords=request.form("keyword")
if search_type="k1" then '按照留言内容搜索
sql="select * from gbook_rec where g_content like '%"&keywords&"%'"
elseif search_type="k2" then
'if search_type="k2" then '按照留言人搜索
sql="select * from gbook_rec where g_name like '%"&keywords&"%'"
else
response.end
end if
set rs=conn.execute(sql)
if rs.eof and rs.bof then%>
<div class="search">没有查找到您要的记录!</div>
<%response.end
else
aResults=rs.getrows()'取出数据放入数组ROW中
application("data")=aResults
set rs=nothing
conn.close '关闭数据库
end if
end if
aResults=application("data")
Dim i,row,pagesize,epage,numb,pagecount,fenye
numb=UBound(aResults,2)+1 '总记录行数
pagesize=2 '每页条数
if numb mod pagesize = 0 then '判断总页数
pagecount=int(numb/pagesize)
else
pagecount=int(numb/pagesize)+1
end if
epage=request.querystring("page")
if epage="" then epage=1
for i=(epage-1)*pagesize to epage*pagesize-1
if i>ubound(aresults,2) or i<0 then exit for%>
<div class="content">
<ul><li>记录<%=i+1%></li>
<li>留言人:<%=aResults(1,i)%></li>
<li>内容:<%=aResults(2,i)%></li>
<li>时间:<%=aResults(3,i)%></li>
<li>IP:<%=aResults(5,i)%></li>
</ul>
</div>
<%Next
if numb>pagesize then
fenye="<a href=search.asp?page=1>首页</a> "
fenye=fenye&"<a href=search.asp?page="&epage-1&" title="&epage-1&">前页</a> "
fenye=fenye&"<a href=search.asp?page="&epage+1&" title="&epage+1&">后页</a> "
fenye=fenye&"<a href=search.asp?page="&pagecount&">末页</a> "
fenye=fenye&"<br>"
fenye=fenye&"总页数"&pagecount&",当前页"&epage&",总记录为:"&numb
response.write fenye
end if%>
</body>
</html>
总结:本人认为以上代码除了初次查询需要连接到数据库外,其他时间都不需要连接数据库,对于大型数据查询或连接人数比较多的时候对资源节省还是很有用处理的!另外将查询数组保存在application对象上,还可以根据application("data")("username")来判断某人查询过什么,不过此代码为初次模型,还有待今后大家一起努力改进!希望大家都顶力关注支持,谢谢!
