在ASP应用程序中加入智能搜索(2)

时间:2007-09-18 13:15:00 


  
接下来,判断 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,CNN,3,2 得到搜索后的记录 
    If RST.BOF And RST.EOF Then 
  %> 
  <font color="#FF0000">未找到任何结果!!!</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="info.asp?ID=<%= RST("ID") %>" target="_blank"><%= RST("U_Name") %></a></font> 
   
  <!-- 显示部分详细内容 --> 
  <font style="font: 9pt 宋体"><%= Left(RST("U_Info"),150) %></font><p> 
  <% 
   RST.MoveNext 
   Wend 
    RST.Close 
    Set RST=Nothing 
   End If 
  End If 
  %>

 在上面的代码中,有一个自定义函数 AutoKey ,该函数是实现智能搜索的核心所在。代码如下:

 <% 
  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 U_Name like %" & strSubKey & "%" 
  strNew2=strNew2 & " or U_Info like %" & strSubKey & "%" 
  Next 
  End Select 
   
    ’得到完整的SQL语句 
   
  AutoKey="Select * from T_Sample where U_Name like %" & strKey & "% or U_Info like %" & strKey & "%" & strNew1 & strNew2 
   
  End Function 
  %> 


 

标签:搜索,智能
0
投稿

猜你喜欢

  • 制作一个更漂亮一些的多选列表框

    2007-10-16 13:04:00
  • 精细分析 SQL server服务器的内存配置

    2009-01-19 13:56:00
  • IE6,7下实现white-space:pre-wrap;

    2009-12-31 18:30:00
  • 启动iis出现发生意外0x8ffe2740的解决方法

    2011-03-31 11:19:00
  • Dreamweaver4使用技巧之--为你的站点定做颜色

    2010-09-05 21:12:00
  • MYSQL教程:保证安全的备份MYSQL数据库的方法

    2010-03-03 17:12:00
  • javascript图片预加载

    2009-08-30 12:47:00
  • 10个美观实用的 jQuery/Mootools 日历插件

    2009-09-17 13:03:00
  • 关于长度单位pt、px、dpi的误解

    2008-06-01 13:30:00
  • innerHTML,outerHTML,innerText,outerText用法

    2008-02-15 12:22:00
  • 二级域名的解析指向ASP源码

    2007-10-19 19:13:00
  • 让复杂导航设计变得简单

    2008-01-07 11:50:00
  • asp最简单的生成验证码代码

    2011-03-07 11:05:00
  • 微软工程师讲解SQL server阻塞

    2008-01-05 14:02:00
  • SWFObject2.0: 基于Javascript的Flash媒体版本检测与嵌入模块

    2009-08-19 13:24:00
  • 如何从SQL数据库中调用图片?

    2009-11-15 19:59:00
  • 解构用户研究

    2010-03-15 12:34:00
  • sql基本查询语句介绍

    2008-05-21 13:58:00
  • 浏览器的字体等宽空格

    2008-08-28 12:25:00
  • 对fieldset和legend的默认样式一点分析

    2008-07-02 12:56:00
  • asp之家 网络编程 m.aspxhome.com