当前位置: 首页 > 图文教程 > 网络编程 > ASP > 使用Filter实现信息的二次检索

ASP
Microsoft 脚本编码器(3) --- 使用脚本编码器
WAP中的ASP技术(一)
WAP中的ASP技术(二)
WAP中的ASP技术(三)
WAP中的ASP技术(四)
在ADO使用SELECT语法一
在ADO使用SELECT语法二
在ADO使用SELECT语法三
在ADO使用SELECT语法四
在ADO使用SELECT语法五
在ADO使用SELECT语法六
Asp+语法介绍(六)----数据库篇
vbscript错误代码及对应解释大全
ASP系列讲座(三)创建 ASP 页
ASP系列讲座(四)使用脚本语言
ASP系列讲座(五)使用变量和常量
ASP系列讲座(六)编写过程
ASP系列讲座(七)使用组件和对象
ASP系列讲座(八)使用集合
ASP系列讲座(九)设置对象作用域

ASP 中的 使用Filter实现信息的二次检索


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 265 ::
收藏到网摘: n/a

思考一个问题:怎么实现在第一次检索的基础上进行二次检索?

通常,我们的做法是第一次检索时保存检索条件,在第二次行检索时组合两次检索条件对数据库进行一次新的查询,如:

第一次检索:Select * from table where age>18

第二次检索:Select * from table where age>18 and name like 'zh%'

这样做虽可以实现我们所要的结果,但效率上个人认为却大打了折扣!

能不能缓存第一次检索的记录集,第二次检索时只在缓存的记录集上进行,而不是重新对数据库进行查询?

RecordSet对象有个属性Filter,它的作用是通过添加条件以控制欲显示的记录集,但并不影响原本的记录集!我们来看下怎么用它实现二次检索:

以下为引用的内容:
<%
Dim oConn,oRs
Set oConn=Server.CreateObject("ADODB.Connection")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("db1.mdb")
Set ors = Server.CreateObject("ADODB.RecordSet")
ors.Open "select * from t1 where age>20",oConn,1,2

Response.Write "一次检索:select * from t1 where age>20<br/>"
Response.Write "----------------------------------<br/><br/>"
Do while not ors.Eof
    Response.Write ors("name") & ":" & ors("age") & "<br/>"
    ors.MoveNext
Loop
Response.Write "总计:" & ors.RecordCount & "<br/>"
Response.Write "----------------------------------<br/><br/>"


Response.Write "二次检索:Filter(name like '王%')<br/>"
Response.Write "----------------------------------<br/><br/>"
ors.Filter = "name like '王%'"
If not(oRs.Eof and ors.Bof) Then ors.MoveFirst
Do while not ors.Eof
    Response.Write ors("name") & ":" & ors("age") & "<br/>"
    ors.MoveNext
Loop
Response.Write "总计:" & ors.RecordCount & "<br/>"
Response.Write "----------------------------------<br/>"

ors.Close
Set ors = Nothing
oConn.Close
Set oConn = Nothing
%>

结果:

但这还有一个问题:很多情况下两次检索并不是同时进行的,而是在第一次检索完成后手动输入条件再进行二次检索,所以我们得想办法在二次检索时第一次检索的记录集仍存在!我们可以用Session对象!将Connection对象和RecordSet对象都保存在Session中,实现如下:

以下为引用的内容:
List.asp:
<%
Set Session("conn")=Server.CreateObject("ADODB.Connection")
Session("conn").Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("db1.mdb")
Set Session("rs") = Server.CreateObject("ADODB.RecordSet")
Session("rs").Open "select * from t1 where age>20",Session("conn"),1,2

Response.Write "一次检索:select * from t1 where age>20<br/>"
Response.Write "----------------------------------<br/><br/>"
Do while not Session("rs").Eof
    Response.Write Session("rs")("name") & ":" & Session("rs")("age") & "<br/>"
    Session("rs").MoveNext
Loop
Response.Write "总计:" & Session("rs").RecordCount & "<br/>"
Response.Write "----------------------------------<br/><br/>"
%>
<form action="search.asp" method="post" name="form1" target="_blank">
  二次检索:
    <input name="f" type="text" id="f">
  <input type="submit" name="Submit" value="提交">
</form>

Search.asp:
<%
Response.Write "二次检索条件:" & Trim(Request("f")) & "<br/>"
Response.Write "----------------------------------<br/><br/>"

Session("rs").Filter = ""
Session("rs").Filter = Trim(Request("f"))
If not(Session("rs").Eof and Session("rs").Bof) Then Session("rs").MoveFirst
do while not Session("rs").Eof
    Response.Write Session("rs")("id") & ":" & Session("rs")("name") & "<br/>"
    Session("rs").MoveNext
loop
Response.Write "总计:" & Session("rs").RecordCount & "<br/>"
Response.Write "----------------------------------<br/>"
%>

 结果:

参考文章:

1.ado多次按条件使用一个记录集中的数据的方法:http://blog.csdn.net/precipitant/archive/2005/08/04/446003.aspx

2.ado 记录集对象的filter属性使用中需注意的地方:http://blog.csdn.net/precipitant/archive/2005/12/13/550979.aspx

原文:http://www.mzwu.com/article.asp?id=1106