用泛型做的分页类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/// <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;
        }

  

posted @   流浪的狸猫  阅读(411)  评论(0编辑  收藏  举报
编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
点击右上角即可分享
微信分享提示