【C#】真分页功能的实现

来源:互联网 发布:gephi输入数据 编辑:程序博客网 时间:2024/06/05 07:43

【前言】

        分页功能是非常常见的,在一般的项目中或多或少都会出现需要将数据库中的信息查询出来展示到前台的功能,但是为了用户体验度往往需要分页显示这些数据。

【实现】

       我们在底层封装一个获取分页数据的方法:

public List<TEntity> GetPagingList<TEntity>(string connectionString, string table, string condition, string orderBy, int pageIndex, int pageSize) where TEntity : class, new()         {             if (string.IsNullOrWhiteSpace(table) || string.IsNullOrWhiteSpace(orderBy))             {                 return new List<TEntity>();             }               string sqlFormat = @"select *                                 from (select *, ROW_NUMBER() over (order by {2}) AS RowNumber                                       from {0}                                       where 1 = 1 {1}) T                                 where RowNumber between @skip and @end                                 order by {2}";               if (!string.IsNullOrEmpty(condition))             {                 condition = "and " + condition;             }               int skip = (pageIndex - 1) * pageSize + 1;             int end = pageIndex * pageSize;               var parameters = new SqlParameter[]             {                 new SqlParameter("@skip", skip),                 new SqlParameter("@end", end)             };               string sqlString = string.Format(sqlFormat, table, condition, orderBy);             return GetList<TEntity>(connectionString, sqlString, parameters);         }  


原创粉丝点击