惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Fortinet All Blogs
B
Blog
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
U
Unit 42
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
Recorded Future
Recorded Future
C
Check Point Blog
V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Full Disclosure
小众软件
小众软件
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
P
Proofpoint News Feed
罗磊的独立博客
量子位
D
Docker
博客园_首页
D
DataBreaches.Net
Project Zero
Project Zero
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - Franky
Security Latest
Security Latest
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
大猫的无限游戏
大猫的无限游戏

博客园 - 张远强

原版SQLHelper.cs下载 Chrome浏览器Proxy Switchy、Tampermonkey等扩展程序下载 GreaseMonkey脚本:阻止Google转换搜索链接地址 站在31岁,理解程序员年过三十这道坎 GreaseMonkey让网站登录验证码形同虚设 DateTime.ToString格式限定符转义 借Windows说明Linux分区和挂载点 JSON解析类(C#) 彻底解决ASP.NET MD5加密中文结果和ASP不一致的问题 SharpZipLib使用示例 DotNetZip使用示例 Dvbbs 8.2.0 RC1多功能编辑器,提供下载 B/S系统版本管理V1.0正式发布 ASP.NET 2.0缓存 个人网站与动网整合步骤(支持PDO1.0) - 张远强 - 博客园 C#中显/隐式实现接口及其访问方法 C#接口范例 巧用escape解决ASP.NET中URL传参乱码问题 使用overflow代替left截取指定长度字符串
使用Filter实现信息的二次检索
张远强 · 2007-12-27 · via 博客园 - 张远强

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

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

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

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

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

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

 1<%
 2Dim oConn,oRs
 3Set oConn=Server.CreateObject("ADODB.Connection")
 4oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("db1.mdb")
 5Set ors = Server.CreateObject("ADODB.RecordSet")
 6ors.Open "select * from t1 where age>20",oConn,1,2
 7
 8Response.Write "一次检索:select * from t1 where age>20<br/>"
 9Response.Write "----------------------------------<br/><br/>"
10Do while not ors.Eof
11    Response.Write ors("name"& ":" & ors("age"& "<br/>"
12    ors.MoveNext
13Loop
14Response.Write "总计:" & ors.RecordCount & "<br/>"
15Response.Write "----------------------------------<br/><br/>"
16
17
18Response.Write "二次检索:Filter(name like '王%')<br/>"
19Response.Write "----------------------------------<br/><br/>"
20ors.Filter = "name like '王%'"
21If not(oRs.Eof and ors.Bof) Then ors.MoveFirst
22Do while not ors.Eof
23    Response.Write ors("name"& ":" & ors("age"& "<br/>"
24    ors.MoveNext
25Loop
26Response.Write "总计:" & ors.RecordCount & "<br/>"
27Response.Write "----------------------------------<br/>"
28
29ors.Close
30Set ors = Nothing
31oConn.Close
32Set oConn = Nothing
33%>

结果:

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

List.asp:

 1<%
 2Set Session("conn")=Server.CreateObject("ADODB.Connection")
 3Session("conn").Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("db1.mdb")
 4Set Session("rs"= Server.CreateObject("ADODB.RecordSet")
 5Session("rs").Open "select * from t1 where age>20",Session("conn"),1,2
 6
 7Response.Write "一次检索:select * from t1 where age>20<br/>"
 8Response.Write "----------------------------------<br/><br/>"
 9Do while not Session("rs").Eof
10    Response.Write Session("rs")("name"& ":" & Session("rs")("age"& "<br/>"
11    Session("rs").MoveNext
12Loop
13Response.Write "总计:" & Session("rs").RecordCount & "<br/>"
14Response.Write "----------------------------------<br/><br/>"
15%>
16<form action="search.asp" method="post" name="form1" target="_blank">
17  二次检索:
18    <input name="f" type="text" id="f">
19  <input type="submit" name="Submit" value="提交">
20</form>

Search.asp:

 1<%
 2Response.Write "二次检索条件:" & Trim(Request("f")) & "<br/>"
 3Response.Write "----------------------------------<br/><br/>"
 4
 5Session("rs").Filter = ""
 6Session("rs").Filter = Trim(Request("f"))
 7If not(Session("rs").Eof and Session("rs").Bof) Then Session("rs").MoveFirst
 8do while not Session("rs").Eof
 9    Response.Write Session("rs")("id"& ":" & Session("rs")("name"& "<br/>"
10    Session("rs").MoveNext
11loop
12Response.Write "总计:" & Session("rs").RecordCount & "<br/>"
13Response.Write "----------------------------------<br/>"
14%>

结果:

参考文章:

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