1、要查询的数据库名称:data.mdb
数据库中表为:data
数据库表data的字段有:title、about
由于无数据添加页面,所以在数据库各字段中应添加数据,方便查询演示。

2、数据库打开文件conn.asp:
<%
Server.scriptTimeout=”10″
connstr=”DBQ=”+server.mappath(“data.mdb”)+”;DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};”
set conn=Server.CreateObject(“ADODB.connection”)
conn.open connstr
%>

3、查询文件search.asp:
<!–#include file=”conn.asp”–>
<form name=”frm_Search” method=”get” action=”Search.asp”>
请输入关键字:
<input type=”text” name=”key” size=”10″>
<input type=”submit” value=”搜索”>
</form>
<%
Dim S_Key,RST,StrSQL
S_Key = Trim(Request(“key”)) ‘得到搜索关键字的值
If S_Key <>”" then
Set RST=Server.CreateObject(“ADODB.RecordSet”)
StrSQL=AutoKey(S_Key) ‘此处使用自定义函数 AutoKey(),该函数为实现智能搜索的核心
RST.Open StrSQL,conn,3,2 ‘得到搜索后的记录

If RST.BOF And RST.EOF Then
%>
<font color=”#FF0000″>Sorry,未找到任何结果!</font>
<%
Else
%>
搜索名称为“<font color=”#FF0000″><%= S_Key %></font>”的项,共找到 <font color=”#FF0000″><%= RST.RecordCount %></font> 项:<p>
<%
While Not RST.EOF
%>
<!– 此处可设为你所需要的链接目标 –>
<font style=”font: 12pt 宋体”><a href=”show.asp?ID=<%= RST(“ID”) %>” target=”_blank”><%= RST(“title”) %></a></font><br>
<!– 显示部分详细内容 –>
<font style=”font: 9pt 宋体”><%= Left(RST(“about”),150) %></font><p>
<%
RST.MoveNext
Wend
RST.Close
Set RST=Nothing
End If
End If
%> <%
Function AutoKey(strKey)
CONST lngSubKey=2
Dim lngLenKey, strNew1, strNew2, i, strSubKey

‘检测字符串的合法性,若不合法则转到出错页。出错页你可以根据需要进行设定。
if InStr(strKey,”=”)<>0 or InStr(strKey,”`”)<>0 or InStr(strKey,”‘”)<>0 or InStr(strKey,” “)<>0 or InStr(strKey,” “)<>0 or InStr(strKey,”‘”)<>0 or InStr(strKey,chr(34))<>0 or InStr(strKey,”")<>0 or InStr(strKey,”,”)<>0 or InStr(strKey,”<”)<>0 or InStr(strKey,”>”)<>0 then
Response.Redirect “error.htm”
End If

lngLenKey=Len(strKey)
Select Case lngLenKey
Case 0 ‘若为空串,转到出错页
Response.Redirect “error.htm”
Case 1 ‘若长度为1,则不设任何值
strNew1=”"
strNew2=”"
Case Else ‘若长度大于1,则从字符串首字符开始,循环取长度为2的子字符串作为查询条件
For i=1 To lngLenKey-(lngSubKey-1)
strSubKey=Mid(strKey,i,lngSubKey)
strNew1=strNew1 & ” or title like ‘%” & strSubKey & “%’”
strNew2=strNew2 & ” or about like ‘%” & strSubKey & “%’”
Next
End Select

‘得到完整的SQL语句
AutoKey=”Select * from data where title like ‘%” & strKey & “%’ or about like ‘%” & strKey & “%’” & strNew1 & strNew2

End Function
%>
<%
conn.Close
Set conn=Nothing
%>

4、查询后显示页面show.asp:
<!–#include file=”conn.asp”–>
<%id=request.querystring(“id”)%>
<%set show=conn.execute(“select*from data where id=”&id&”")%>
<hr size=”1″>
<table border=”1″ cellpadding=”0″ cellspacing=”0″ style=”border-collapse: collapse” bordercolor=”#111111″ width=”100%” height=”180″>
<tr>
<td width=”50%” height=”22″><b>标题:</b><%=show(“title”)%></td>
<td width=”50%” height=”22″><b>关于:</b><%=show(“about”)%></td>
</tr>
</table>
<%set show=nothing%>

5、检索出错文件error.htm
<html>
<head>
<title>
出错啦!
</title>
</head>
<body>
<center>对不起,您要检索的信息字符串不合法则!<br>请<a href=”javascript:history.back(1)”>返回</a>重新检索!</center>
</body>
</html>

说明:将以上文件和数据库放在同一目录下.也可以将数据库文件data.mdb放在其他文件夹,但注意修改conn.asp相应的数据库地址。
=====================================================================================================================================

ASP查询代码

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据