<html>
<head>
 <title>多个关键字的模糊查询示例</title>
</head>
<body><p>多个关键字之间用空格或逗号分开</p>
<form name = "form1" action =""  method = "post">
<p>请输入关键字：<input type = "text" name ="key" size = "20">
<input type ="submit" name = submit value = "确定"></p>
</form>
<%
If Request.Form("submit")<>"" Then
 keywords = Trim(Request.Form("key"))        '将查询关键字前后空格删除
  If keywords = "" Then
    response.Clear()
	Response.Write "请<a href=9-10.asp>返回</a>输入查询搜索关键字！"
	Response.End
  End If
  '下面建立Connection对象
  Dim cnn,strConn
  strConn="Dbq=" & Server.Mappath("jxgl.mdb") & _
  ";Driver={Microsoft Access Driver (*.mdb)}"
  Set cnn=Server.CreateObject("ADODB.Connection") 
  cnn.Open strConn
  '建立查询字符串
  keywords = Replace(keywords,"，"," ") 	'将获得的字符串中中文逗号换成空格
  keywords = Replace(keywords,","," ")  '将获得的字符串中英文逗号换成空格
  strsql = "Select * from kc where "    '关键字匹配sql 语句
  keywordarray = Split(keywords)        '将输入的字符串根据空格分开成数组
  max = UBound(keywordarray)         '得到输入的关键字个数
  If max = 0 Then                     '如果max 等于0,说明只输入了一个关键字
    strsql =strsql&"课程名称 like '%"&keywordarray(0)&"%'"   '模糊搜索
  Else	
    '如果含有多个关键字，采用循环处理sql 语句
    For i = 0 to max-1
	if trim(keywordarray(i))<>"" then
       strsql = strsql&"课程名称 like '%"&keywordarray(i)&"%' or "
	end if
    Next
    strsql = strsql&"课程名称 like '%"&keywordarray(max)&"%'"
  End If
  '创建RecordSet 对象
  Set rs = Server.CreateObject("ADODB.RecordSet")
  rs.Open strsql,cnn,1,3             '执行查询，结果保存在rs中
  If rs.EOF or rs.BOF Then           '不存在记录
    Response.Write("没有你要找的内容")
  Response.End
  Else                                   '存在记录
   Do While Not rs.EOF                '循环显示所有记录的id
	id = rs("id")
    %>
    <%=rs("id")%>&nbsp;&nbsp;<%=rs("课程名称")%><br>
    <%
    rs.MoveNext
   Loop
  End If
End If
%>
</body>
</html>
