Google搜索的实现

创建一个web服务

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
//引入空间
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// KeyFind 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//添加服务脚本(必须添,否则程序不能正常运行)
[System.Web.Script.Services.ScriptService]
public class KeyFind1 : System.Web.Services.WebService
{

public KeyFind1()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
//定义数组保存获取的内容
private string[] autoCompleteWordList = null;
public SqlConnection conn = null;

//两个参数“prefixText”表示用户输入的前缀,count表示返回的个数
[WebMethod]
public String[] GetCompleteDepart(string prefixText, int count)
{
///检测参数是否为空
if (string.IsNullOrEmpty(prefixText) == true || count <= 0) return null;
// 如果数组为空
if (autoCompleteWordList == null)
{
conn = new SqlConnection("server=.\\SQLEXPRESS;database=Sale;User ID=sa;pwd=123");

SqlDataAdapter da = new SqlDataAdapter("select 商品名称 from 商品表 where 商品名称 like '%" + prefixText + "%'", conn);

DataSet ds = new DataSet();
da.Fill(ds);
//conn.Close();
//读取内容文件的数据到临时数组
string[] temp = new string[ds.Tables[0].Rows.Count];
int i = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
temp[i] = dr["商品名称"].ToString();
i++;
}
Array.Sort(temp, new CaseInsensitiveComparer());
//将临时数组的内容赋给返回数组
autoCompleteWordList = temp;
}
//定位二叉树搜索的起点
int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
if (index < 0)
{ //修正起点
index = ~index;
}
//搜索符合条件的数据
int matchCount = 0;
for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
{ ///查看开头字符串相同的项
if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
{
break;
}
}
//处理搜索结果
string[] matchResultList = new string[matchCount];
if (matchCount > 0)
{ //复制搜索结果
Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);
}
return matchResultList;
}
}

html代码


<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
 
<cc1:AutoCompleteExtender ID="AutoCompleteExtender3" runat="server" TargetControlID="TextBox1"
ServicePath="KeyFind1.asmx" CompletionSetCount="10" MinimumPrefixLength="1" ServiceMethod="GetCompleteDepart">
</cc1:AutoCompleteExtender>
<table style="width: 507px; height: 156px">
<tr>
<td style="width: 1027px">
</td>
<td style="width: 396px; text-align: center">
<strong><span><span style="font-size: 16pt">搜Asp.net技术<span>(搜索引擎)</span></span></span></strong></td>
<td>
</td>
</tr>
<tr>
<td style="width: 1027px; text-align: center; height: 57px;">
<span style="font-size: 9pt">
请输入查询关键字:</span></td>
<td style="width: 396px; height: 57px;">
<asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox></td>
<td style="height: 57px">
<asp:Button ID="Button1" runat="server" Text="搜 索" /></td>
</tr>
<tr>
<td style="width: 1027px">
</td>
<td style="width: 396px">
</td>
<td>
</td>
</tr>
</table>
</form>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值