public void ProcessRequest(HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); context.Response.ContentType = "text/json"; int start = 0; int limit = 10; string sort = string.Empty; string dir = string.Empty; string query = string.Empty; if (!string.IsNullOrEmpty(context.Request["start"])) { start = int.Parse(context.Request["start"]); } if (!string.IsNullOrEmpty(context.Request["limit"])) { limit = int.Parse(context.Request["limit"]); } if (!string.IsNullOrEmpty(context.Request["sort"])) { sort = context.Request["sort"]; } if (!string.IsNullOrEmpty(context.Request["dir"])) { dir = context.Request["dir"]; } if (!string.IsNullOrEmpty(context.Request["query"])) { query = context.Request["query"].ToUpper(); } Paging <PracInfo> PracInfos = PracInfo.PracInfosPaging(start, limit, sort, dir, query); context.Response.Write(string.Format("{{total:{1},'PracInfos':{0}}}", JSON.Serialize(PracInfos.Data), PracInfos.TotalRecords)); }
public static Paging <PracInfo> PracInfosPaging(int start, int limit, string sort, string dir, string filter) { List <PracInfo> pracInfos = PracInfo.Getpracname; if (filter.Length > 0) { pracInfos.Clear(); DBMysql db = new DBMysql(); List <PracInfo> data = new List <PracInfo>(); string sql = "SELECT PY, HZ, ZM FROM pinyin "; if (filter != "*") { sql += "WHERE PY LIKE '%" + filter + "%' AND ZM='" + filter.Substring(0, 1) + "' "; } DataTable dt = db.Query(sql); if (dt.Rows.Count > 0) //使用拼音輸入 { sql = "SELECT acclv_fname, acclv_stfcode FROM access_level WHERE (1=0 "; for (int i = 0; i < dt.Rows.Count; i++) { sql += "OR acclv_fname LIKE '%" + dt.Rows[i]["HZ"].ToString() + "%' "; } sql += ") "; DataTable dt1 = db.Query(sql); if (dt1.Rows.Count > 0) { for (int j = 0; j < dt1.Rows.Count; j++) { PracInfo pracInfo = new PracInfo(); pracInfo.fname = dt1.Rows[j]["acclv_fname"].ToString(); pracInfo.stfcode = dt1.Rows[j]["acclv_stfcode"].ToString(); pracInfos.Add(pracInfo); } } dt1.Dispose(); } else //直接輸入中文字 { sql = "SELECT acclv_fname, acclv_stfcode FROM access_level "; sql += "WHERE acclv_fname LIKE '%" + filter + "%' "; dt = db.Query(sql); if (dt.Rows.Count > 0) { for (int j = 0; j < dt.Rows.Count; j++) { PracInfo pracInfo = new PracInfo(); pracInfo.fname = dt.Rows[j]["acclv_fname"].ToString(); pracInfo.stfcode = dt.Rows[j]["acclv_stfcode"].ToString(); pracInfos.Add(pracInfo); } } } dt.Dispose(); db.Close(); } if (!string.IsNullOrEmpty(sort)) { pracInfos.Sort(delegate(PracInfo x, PracInfo y) { object a; object b; int direction = dir == "DESC" ? -1 : 1; a = x.GetType().GetProperty(sort).GetValue(x, null); b = y.GetType().GetProperty(sort).GetValue(y, null); return(CaseInsensitiveComparer.Default.Compare(a, b) * direction); }); } if ((start + limit) > pracInfos.Count) { limit = pracInfos.Count - start; } List <PracInfo> rangePracInfos = (start < 0 || limit < 0) ? pracInfos : pracInfos.GetRange(start, limit); return(new Paging <PracInfo>(rangePracInfos, pracInfos.Count)); }