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" />
            &nbsp;    
        <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>