用户登录
用户注册

分享至

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

  • 作者: 公交车上玩车震
  • 来源: 51数据库
  • 2021-08-19
首先,了解下原理。
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 >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&"%' "

当然通过区号的查询还没添加,你可以自己试着完善,或者来个混合查询、单独查询、模糊查询以及精确查询的大综合了。
调试页面参看。
软件
前端设计
程序设计
Java相关