思考一个问题:怎么实现在第一次检索的基础上进行二次检索? 中.国站长站
通常,我们的做法是第一次检索时保存检索条件,在第二次行检索时组合两次检索条件对数据库进行一次新的查询,如: Www^Chinaz^com
第一次检索:Select * from table where age>18
中.国.站.长.站
第二次检索:Select * from table where age>18 and name like 'zh%'
[中国站长站]
这样做虽可以实现我们所要的结果,但效率上个人认为却大打了折扣! 中国站.长站
能不能缓存第一次检索的记录集,第二次检索时只在缓存的记录集上进行,而不是重新对数据库进行查询? Www_Chinaz_com
RecordSet对象有个属性Filter,它的作用是通过添加条件以控制欲显示的记录集,但并不影响原本的记录集!我们来看下怎么用它实现二次检索: Www_Chinaz_com
| 以下为引用的内容: <% 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/>"
Www@Chinaz@com ors.Close Set ors = Nothing oConn.Close Set oConn = Nothing %> Www^Chinaz^com |
中.国.站.长.站 结果:
Chinaz^com
中国站长_站,为中文网站提供动力