Ejemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            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 <Patinfo> Patinfos = Patinfo.PatinfosPaging(start, limit, sort, dir, query);

            context.Response.Write(string.Format("{{total:{1},'Patinfos':{0}}}", JSON.Serialize(Patinfos.Data), Patinfos.TotalRecords));
        }
Ejemplo n.º 2
0
        public static Paging <Patinfo> PatinfosPaging(int start, int limit, string sort, string dir, string filter)
        {
            //List<Patinfo> patinfos = Patinfo.Getpatname;
            List <Patinfo> patinfos = new List <Patinfo>();

            if (filter.Length > 0)
            {
                patinfos.Clear();
                DBMysql        db   = new DBMysql();
                List <Patinfo> data = new List <Patinfo>();

                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 pif_ic, pif_name FROM pat_info WHERE (1=0 ";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sql += "OR pif_name 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++)
                        {
                            Patinfo patinfo = new Patinfo();
                            patinfo.patic   = dt1.Rows[j]["pif_ic"].ToString();
                            patinfo.patname = dt1.Rows[j]["pif_name"].ToString();
                            patinfos.Add(patinfo);
                        }
                    }
                    dt1.Dispose();
                }
                else
                {
                    sql  = "SELECT pif_ic, pif_name FROM pat_info "; //直接輸入中文字
                    sql += "WHERE pif_name LIKE '%" + filter + "%' ";
                    dt   = db.Query(sql);
                    if (dt.Rows.Count > 0)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            Patinfo patinfo = new Patinfo();
                            patinfo.patic   = dt.Rows[j]["pif_ic"].ToString();;
                            patinfo.patname = dt.Rows[j]["pif_name"].ToString();
                            patinfos.Add(patinfo);
                        }
                    }
                }
                dt.Dispose();
                db.Close();
            }

            if (!string.IsNullOrEmpty(sort))
            {
                patinfos.Sort(delegate(Patinfo x, Patinfo 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) > patinfos.Count)
            {
                limit = patinfos.Count - start;
            }
            List <Patinfo> rangePatinfos = (start < 0 || limit < 0) ? patinfos : patinfos.GetRange(start, limit);

            return(new Paging <Patinfo>(rangePatinfos, patinfos.Count));
        }