当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP编程入门进阶(二十):ADO组件之查询数据记录

ASP
ASP调用ORACLE存储过程并返回结果集
用ASP实现网页BBS
关于Global.asa文件的深入研究与session变量失效提示的具体方法
简易ASP+注册系统
防护手册:如何防止ASP木马在服务器上运行
用Visual Basic实现多画面播放功能之二
如何增强ASP程序性能(1)
如何增强ASP程序性能(2)
如何增强ASP程序性能(3)
ASP备份数据库
二十八条改善 ASP 性能和外观的技巧
在Form域中Post大于100K的数据
如何使用ASP制作模似动态生长的表单?
Microsoft IIS 真的如此「不安全」吗?(1)
Microsoft IIS 真的如此「不安全」吗?(2)
Microsoft IIS 真的如此「不安全」吗?(3)
Microsoft IIS 真的如此「不安全」吗?(4)
Microsoft IIS 真的如此「不安全」吗?(5)
关于页面和代码分离
ServerVariables 对路径的操作

ASP编程入门进阶(二十):ADO组件之查询数据记录


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

首先,了解下原理。
1,提供文本框进行查询内容的输入
2,将查询信息提交页面程序处理
3,程序页主要作用:接受查询信息,根据此信息调用特定的SQL查询语句,得出查询结果并能显示。
其实,主要精髓就是SQL语句的写法上。
之前的提取为 "select * form whattable where id="&id
插入为 "insert into whattable(xx_rs) values(' "&content&" ')"
删除为 "delete from whattable where id="&id
修改为 "update whattable set xx_rs=' "&log_content&" ' where id="&id
查询为 "select * form whattable where xx_rs like '%"&wahtkey&"%' "
下面通过一个例题来研究下
1,建立数据库zipcode.mdb中的zip表
字段id,类型自动编号(关键字)
字段placename,类型文本
字段province,类型文本
字段zipcode,类型文本
字段borough,类型文本
2,加入数据库信息内容
id 自动编号,无需加入
placename 对应县市
province 对应省份
zipcode 对应邮政编码
borough 对应区号
3,连接文件conn.asp

<%
db_path = "zipcode.mdb"
Set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath(db_path)
conn.Open connstr
%>

4,查询输入页search.asp

[Ctrl+A 全部选择进行拷贝 提示:可先修改部分代码,再点击运行]
5,信息查询页,同样是search.asp

<!--#include file="conn.asp" -->
<%
if request.form("submit")="search" then
whatzip=request.form("zipcode")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from zip where zipcode like '%"&whatzip&"%' "
rs.Open sql,conn,1,1
%>
<%
if rs.EOF and rs.BOF then
response.write ("未能查到")
else
Do Until rs.EOF
response.write("<hr>该地址是:"& rs("placename")&rs("zipcode"))
response.write("<br>所在省份是:"& rs("province"))
rs.MoveNext
Loop
end if
%>
<br><a href="search.asp">again</a>
<%
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
else
%>
<form action="search.asp" method="post">
<input type="text" name="zipcode">
<input type="submit" name="submit" value="search">
</form>
<%end if%>

以上采用like意思表示进行模糊查询,要精确查询则直接使用
sql = "Select * from zip where zipcode = '%"&whatzip&"%' "
当然通过区号的查询还没添加,你可以自己试着完善,或者来个混合查询、单独查询、模糊查询以及精确查询的大综合了。
调试页面参看。