搜索智能感知效果(调用数据库数据)

来源:互联网 发布:网络的利与弊作文800字 编辑:程序博客网 时间:2024/06/18 11:43

---------------------aspx页面-前台------------------------------

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/Jquery1.7.js" type="text/javascript"></script>
    <script src="js/jquery.autocomplete.js" type="text/javascript"></script>
    <script src="js/jquery.autocomplete.pack.js" type="text/javascript"></script>
    <link href="js/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
      <script type="text/javascript">
          $(function () {
              $.ajax({
                  type: "post",
                  contentType: "application/json",
                  url: "WebService1.asmx/GetGoods",
                  data: "{}",
                  success: function (result) {
                      var array = new Array();
                      for (var i = 0; i < result.d.length; i++) {
                          array.push(result.d[i]);
                      }
                      $('#txtQuery').autocomplete(array);
                  }
              })
          })
    </script>


</head>
<body>
      <form id="form1" runat="server">
    <div>
        <input type="text" id="txtQuery" />
    </div>
    </form>


</body>
</html>

--------------------------- WebServices-------------------------------------

// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public List<string> GetGoods()
        {
            List<string> list = null;
            if (Context.Cache["goods"] == null)
            {
                string strcon = ConfigurationManager.ConnectionStrings["sqlservercon"].ConnectionString;
                SqlConnection conn = new SqlConnection(strcon);
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select GoodsTitle FROM T_Search";
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                list = new List<string>();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        list.Add(reader["GoodsTitle"].ToString());
                    }
                }
                reader.Dispose();
                cmd.Dispose();
                conn.Dispose();
                Context.Cache["goods"] = list;
            }
            else
            {
                list = Context.Cache["goods"] as List<string>;
            }


            return list;
        } 

    }

-----------------------Web.config--------------------------------

<connectionStrings>
    <add name="sqlservercon" connectionString="Data Source=.;Initial Catalog=lianxi;Persist Security Info=True;User ID=sa;Password=111111"/>
    
  </connectionStrings>

原创粉丝点击