关于asp查询由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“asp数据查询”。
1、要查询的数据库名称:data.mdb
数据库中表为:data
数据库表data的字段有:title、about
由于无数据添加页面,所以在数据库各字段中应添加数据,方便查询演示。
2、数据库打开文件conn.asp:
<%
Server.scriptTimeout=“10”
connstr=“DBQ=”+server.mappath(“data.mdb”)+“;DefaultDir=;DRIVER={Microsoft Acce Driver(*.mdb)};”
set conn=Server.CreateObject(“ADODB.connection”)
conn.open connstr
%>
3、查询文件search.asp:
请输入关键字:
<%
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
%>
Sorry,未找到任何结果!
<%
Else
%>
搜索名称为“<%= S_Key %>”的项,共找到 <%= RST.RecordCount %> 项:
<%
While Not RST.EOF
%>
“ target=”_blank“><%= RST(”title“)%>
<%= Left(RST(”about“),150)%>
<%
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 & strNew
2End Function
%>
<%
conn.Close
Set conn=Nothing
%>
4、查询后显示页面show.asp:
<%id=request.querystring(“id”)%>
<%set show=conn.execute(“select*from data where id=”&id&“”)%>
标题:<%=show(“title”)%>关于:<%=show(“about”)%>
<%set show=nothing%>
5、检索出错文件error.htm
出错啦!
对不起,您要检索的信息字符串不合法则!请返回重新检索!