我有一个带有 JQuery 的 ASP.NET 文本框自动完成功能,工作正常,我键入部分人名并显示包含所有可能选项的列表,但现在我希望当我搜索名称时,我会在结果列表中显示姓名旁边的电子邮件地址,姓名和电子邮件在 SQL 数据库中,我希望当我输入 John 时,我会得到列表
约翰·安德鲁斯 John@email.com
约翰·史密斯 Smith@email.com
等等......这是代码:
<%@ WebHandler Language="C#" Class="Search_CS" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
public class Search_CS : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string prefixText = context.Request.QueryString["q"];
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from directory where name like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
StringBuilder sb = new StringBuilder();
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
sb.Append(sdr["name"])
.Append(Environment.NewLine);
}
}
conn.Close();
context.Response.Write(sb.ToString());
}
}
}
public bool IsReusable {
get {
return false;
}
}
}