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

ASP
ASP视频教程:后台页面加入限制访问和禁用缓存功能
ASP视频教程:制作网站前台首页
ASP视频教程:备份和还原SQL Server 2000数据库
ASP实例教程:asp无限级显示分类代码
IIS无法启动错误的几种情况汇总
asp Http_Referer,Server_Name和Http_Host
ASP 调用带参数输出的COM接口
隐藏修改文件时间和文件属性的ASP脚本
ASP Crazy 模版操作类(最简单的模板类、仅提供交流)
asp 动态数组 提供Add、Insert、Remove、RemoveAt、Search等方法。
asp 取一个数的整数 但不是四舍五入,只要有小数,就取大于这个数的整数
asp 判断上传文件中是否存在危险代码
asp 获取url函数小结
ASP 调用dll及封装dll实例
asp 实现的冒泡排序程序
asp 自定义分段函数/求第N名成绩
ASP 高级模板引擎实现类
ASP 常见的连接字符串写法(access2007)
ASP向Excel导数据(图片)终结版 ASP操作Excel
ASP实现防止网站被采集代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 253 ::
收藏到网摘: 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&"%' "
当然通过区号的查询还没添加,你可以自己试着完善,或者来个混合查询、单独查询、模糊查询以及精确查询的大综合了。
调试页面参看。