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

  首先,了解下原理。

  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&"%' "

  当然通过区号的查询还没添加,你可以自己试着完善,或者来个混合查询、单独查询、模糊查询以及精确查询的大综合了。

  调试页面参看。