public string[] GetString(string prefixText, int count){ System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(count); System.Data.DataSet ds = new System.Data.DataSet(); //這里是我在數據庫中取數據的代碼 其中SqlHelper類是項目中的取數據基類 //string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText); //ds = SqlHelper.Query(strSql); //for (int i = 0; i < ds.Tables[0].Rows.Count; i++) //{ // list.Add(ds.Tables[0].Rows[i][0].ToString()); //} for (int i = 0; i < count; i++) { list.Add(prefixText+i.ToString()); } return list.ToArray(); }
webService代碼: using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; /// <summary> /// AutoComplete 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //下面是必須的,否則功能無法實現 [System.Web.Script.Services.ScriptService] public class AutoComplete : System.Web.Services.WebService { public AutoComplete () { //如果使用設計的組件,請取消注釋以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string[] GetString(string prefixText, int count){ System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(count); System.Data.DataSet ds = new System.Data.DataSet(); //這里是我在數據庫中取數據的代碼 其中SqlHelper類是項目中的取數據基類 //string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText); //ds = SqlHelper.Query(strSql); //for (int i = 0; i < ds.Tables[0].Rows.Count; i++) //{ // list.Add(ds.Tables[0].Rows[i][0].ToString()); //} for (int i = 0; i < count; i++) { list.Add(prefixText+i.ToString()); } return list.ToArray(); } } 有哪里不對的地方還請大家指教