码迷,mamicode.com
首页 > 其他好文 > 详细

用泛型做的分页类

时间:2014-09-24 16:03:17      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:des   blog   os   ar   for   div   sp   on   c   

/// <summary>
        /// 分页
        /// </summary>
        /// <param name="num">每页的数量</param>
        /// <param name="page">页码</param>
        /// <param name="model">参数列表(用于查询)</param>
        /// <returns></returns>
        public static List<T> GetPagers(int num, int page, T model)
        {
            List<T> tList = new List<T>();
            string name = new T().GetType().Name;
            PropertyInfo[] proList = model.GetType().GetProperties();
            string sqlStr = "select top " + num + "  * from " + name;
            string whereStr = " ";
            foreach (PropertyInfo pi in proList)
            {
                if (DbHelperSQL.IsPrimaryKey(pi.Name, name) == true)
                {
                    sqlStr = sqlStr + " where " + pi.Name + " not in (select top (" + num + " * (" + page + "-1))" + pi.Name + " from " + name + " order by OrderDate desc)";
                }
            }


            foreach (PropertyInfo pi in proList)
            {
                if (pi.GetValue(model, null) != null && DbHelperSQL.IsPrimaryKey(pi.Name, name) == false)
                {
                    string piValue = pi.GetValue(model, null).ToString();
                    if (piValue != "False" && piValue != "0001/1/1 0:00:00" && piValue != "0")
                    {
                        whereStr += " and " + pi.Name + "=‘" + pi.GetValue(model, null).ToString() + "‘ order by OrderDate desc";
                    }
                }
            }

            string sql = sqlStr + whereStr;
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];


            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] list = t.GetType().GetProperties();
                foreach (PropertyInfo pi in list)
                {
                    string piName = pi.Name;
                    if (dt.Columns.Contains(piName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[piName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                tList.Add(t);
            }
            return tList;
        }

  

用泛型做的分页类

标签:des   blog   os   ar   for   div   sp   on   c   

原文地址:http://www.cnblogs.com/liuchang/p/3990615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!