Ajax AutoComplete 在Sql数据库中读取数据

前台 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
             </asp:ScriptManager> 
             <ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
         TargetControlID="TextBox1" 
         CompletionSetCount="12" 
         EnableCaching="true" 
         MinimumPrefixLength="1" 
         ServicePath="sql_test.asmx" 
        CompletionListElementID="Panel1" 
         ServiceMethod="GetCompleteList"> 
             </ajaxToolkit:AutoCompleteExtender> 
         </div> 
         &nbsp; 
         <asp:TextBox ID="TextBox1" runat="server" ForeColor="Black"> </asp:TextBox> 

sql_test.asmx的代码 

using System; 
using System.Web.Services; 
using System.Collections.Generic; 
using System.Data.SqlClient; 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.Web.Script.Services.ScriptService] 
public class sql_test : System.Web.Services.WebService { 

     public sql_test () { 

         //如果使用设计的组件,请取消注释以下行 
         //InitializeComponent(); 
     } 

     [WebMethod] 
     public string[] GetCompleteList(string prefixText, int count) 
     { 
         List <string> items = new List <string> (count);//泛型 
         SqlConnection myCon = new SqlConnection("Server=AAAA;uid=sa;pwd=TTiger;Database=kmjxc_08_test");//数据库连接 
         myCon.Open();//打开数据库连接 
         SqlCommand myCmd = new SqlCommand("select top " + count + " username from user_item where username like '" + prefixText + "%'group by username order by username ", myCon); 
         SqlDataReader myDR = myCmd.ExecuteReader(); 
         while (myDR.Read()) 
         { 
             items.Add(myDR["username"].ToString()); 
         } 
         myCon.Close();//关闭数据库连接 
         return items.ToArray(); 
     } 
     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值