用户名: 密码: 验证码: 注册           网站地图 高级搜索 RSS订阅 收藏本站
山东十七地市站长联盟信息: 济南 青岛 淄博 枣庄 东营 烟台 潍坊 济宁 泰安 威海 日照 莱芜 临沂 德州 聊城 滨州 菏泽      
您的位置:主页>网络编程>Asp编程>

ASP进阶:用asp做的简单搜索引擎代码

[ 来源: | 作者: | 更新日期:2008-5-26 16:45:25 | 评论 0 条 | 我要投稿 ]

下面是库中URLINDEX表:URL和Keywords字段分别添加了索引. 字串2

URL           文本 (索引:有(无重复))
Title            文本
Description 文本
Summary    文本
Keywords   文本(索引:有(无重复))

字串5

doquery.asp

字串9

<HTML><HEAD><TITLE>简单搜索引擎</TITLE></HEAD>
<BODY BGCOLOR=#ffffff MARGINWIDTH="0" MARGINHEIGHT="0"
LEFTMARGIN=0 TOPMARGIN=0> 字串1

<FORM METHOD="post" ACTION="doquery.asp?act=search">
 Query: <INPUT TYPE="Text" NAME="QueryString"><BR>
 <INPUT TYPE="Submit" VALUE="Submit">
</FORM>
</CENTER>

字串4


<%
dim act
act=request("act")
if(act="search") then
 QueryString = Request.form( "QueryString" )
 QueryWords  = Split( QueryString )
 strIndent   = "          "
 
 ' 如果搜索为空则返回
 If QueryString = "" Then
  Response.Redirect( "default.asp" )
 End If
 
 Session.timeout = 2
 If IsObject(Session("sitesearch_conn")) Then
     Set conn = Session("sitesearch_conn")
 Else
     Set conn = Server.CreateObject("ADODB.Connection")
     conn.open "driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath("database/SiteSearch.mdb"),"",""
     Set Session("sitesearch_conn") = conn

字串3


 End If
字串8

 ' 查询语句
 sql = "SELECT * FROM [URLIndex] WHERE"
   
字串4


 '搜索Description字段
 sql = sql & " ( [Description] LIKE '%" & QueryWords( 0 ) & "%'"   ' First
 For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
  If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
   If uCase( QueryWords( i-1 ) ) = "OR" Then
    sql = sql & " OR [Description] LIKE '%" & QueryWords( i ) & "%'"
   Else
    sql = sql & " AND [Description] LIKE '%" & QueryWords( i ) & "%'"
   End If
  End If
 Next
字串8

 ' 搜索Keywords字段
 sql = sql & " ) OR ( [Keywords] LIKE '%" & QueryWords( 0 ) & "%'"
 For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
  If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
   If uCase( QueryWords( i-1 ) ) = "OR" Then
    sql = sql & " OR [Keywords] LIKE '%" & QueryWords( i ) & "%'"
   Else
    sql = sql & " AND [Keywords] LIKE '%" & QueryWords( i ) & "%'"
   End If
  End If
 Next

字串4


 '  搜索Title字段 
 sql = sql & " ) OR ( [Title] LIKE '%" & QueryWords( 0 ) & "%'"
 For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
  If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
   If uCase( QueryWords( i-1 ) ) = "OR" Then
    sql = sql & " OR [Title] LIKE '%" & QueryWords( i ) & "%'"
   Else
    sql = sql & " AND [Title] LIKE '%" & QueryWords( i ) & "%'"
   End If
  End If
 Next

字串6


 ' 搜索Summary字段
 sql = sql & " ) OR ( [Summary] LIKE '%" & QueryWords( 0 ) & "%'"
 For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
  If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
   If uCase( QueryWords( i-1 ) ) = "OR" Then
    sql = sql & " OR [Summary] LIKE '%" & QueryWords( i ) & "%'"
   Else
    sql = sql & " AND [Summary] LIKE '%" & QueryWords( i ) & "%'"
   End If
  End If
 Next

字串3

 sql = sql & " )"

字串9


    '
    Set rs = Server.CreateObject("ADODB.Recordset")
    rs.Open sql, conn, 3, 3    
   
    Response.Write "<BR><B> 你搜索的是: </B> " & QueryString   
   
    Response.Write "<BR><B> 搜索的关键字: </B> "
 For i = LBound( QueryWords ) to UBound( QueryWords )
  Response.Write "<BR>" & strIndent & i & ": " & QueryWords( i )
 Next

字串7

    ' Print the SQL String
    Response.Write "<BR><B> sql 语句 : </B> " & sql
 
 ' Print the Results
    Response.Write "<BR><B> 结果    : </B> <UL>"
 On Error Resume Next
 rs.MoveFirst
 Do While Not rs.eof
  Response.Write "<BR>" & "<A HREF='OpenPage.asp?IndexURL=" & rs.Fields("URL").Value & "'>" & rs.Fields("Title") & "</A> - "
  Response.Write rs.Fields("Description") & "<BR>"
  Response.Write "     <FONT SIZE=2>URL: " & rs.Fields("URL") & "</FONT>"
  Response.Write "<HR SIZE=1 WIDTH=200 ALIGN=LEFT>"
  rs.MoveNext 字串6
 Loop
 Response.Write "</UL>"
 
end if  
%>

字串7


</BODY>
</HTML>

字串5


Tags:搜索引擎 代码 简单 QueryWords sql If OR LIKE Response.Write
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为