ASP編程入門進階(二十):ADO組件之查詢數(shù)據(jù)記錄
更新時間:2007年01月03日 00:00:00 作者:
首先,了解下原理。
1,提供文本框進行查詢內(nèi)容的輸入
2,將查詢信息提交頁面程序處理
3,程序頁主要作用:接受查詢信息,根據(jù)此信息調(diào)用特定的SQL查詢語句,得出查詢結(jié)果并能顯示。
其實,主要精髓就是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,建立數(shù)據(jù)庫zipcode.mdb中的zip表
字段id,類型自動編號(關(guān)鍵字)
字段placename,類型文本
字段province,類型文本
字段zipcode,類型文本
字段borough,類型文本
2,加入數(shù)據(jù)庫信息內(nèi)容
id 自動編號,無需加入
placename 對應縣市
province 對應省份
zipcode 對應郵政編碼
borough 對應區(qū)號
3,連接文件conn.asp
4,查詢輸入頁search.asp
[Ctrl+A 全部選擇進行拷貝 提示:可先修改部分代碼,再點擊運行]
5,信息查詢頁,同樣是search.asp
以上采用like意思表示進行模糊查詢,要精確查詢則直接使用
sql = "Select * from zip where zipcode = '%"&whatzip&"%' "
當然通過區(qū)號的查詢還沒添加,你可以自己試著完善,或者來個混合查詢、單獨查詢、模糊查詢以及精確查詢的大綜合了。
調(diào)試頁面參看。
1,提供文本框進行查詢內(nèi)容的輸入
2,將查詢信息提交頁面程序處理
3,程序頁主要作用:接受查詢信息,根據(jù)此信息調(diào)用特定的SQL查詢語句,得出查詢結(jié)果并能顯示。
其實,主要精髓就是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,建立數(shù)據(jù)庫zipcode.mdb中的zip表
字段id,類型自動編號(關(guān)鍵字)
字段placename,類型文本
字段province,類型文本
字段zipcode,類型文本
字段borough,類型文本
2,加入數(shù)據(jù)庫信息內(nèi)容
id 自動編號,無需加入
placename 對應縣市
province 對應省份
zipcode 對應郵政編碼
borough 對應區(qū)號
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&"%' "
當然通過區(qū)號的查詢還沒添加,你可以自己試著完善,或者來個混合查詢、單獨查詢、模糊查詢以及精確查詢的大綜合了。
調(diào)試頁面參看。