DeveloperSharp?高效分页使用详解(实现分页功能的多种原理方案)快来看

随心笔谈12个月前发布 admin
119 0



目录正文◆◆第一种方案如下◆◆◆◆第二种方案如下◆◆

支持.Net/.Net Core/.Net Framework,可以部署在Docker, Windows, Linux, Mac。

数据分页,几乎是任何应用系统的必备功能。但当数据量较大时,分页操作的效率就会变得很低。大数据量分页时,一个操作耗时5秒、10秒、甚至更长时间都是有可能的,但这在用户使用的角度是不可接受的……

数据分页往往有三种常用方案。

第一种,把数据库中存放的相关数据,全部读入代码/内存,再由代码对其进行分页操作。

第二种,直接在数据库中对相关数据进行分页操作,再把分页后的数据输出给代码程序。

第三种,先把数据库中的相关数据全部读入“缓存”,再由代码程序对“缓存”中的数据进行读取+分页操作。

本文下面重点阐述第一种与第二种两个解决方案,它们也都是直接基于“数据库”的。

(第三种方案虽然速度较快,但由于需要用到“缓存”这类第三方工具,且在有数据更改时需要较复杂的“数据库-缓存”同步操作,故本文暂不详述。)

从NuGet引入DeveloperSharp包,初始化IUtility工具

using DeveloperSharp.Framework.CoreUtility;
————————–
IUtility IU=new Utility();

IUtility内构建有3个重载的分页PagePartition方法:

PagePartition

声明:PagePiece PagePartition(DataTable Table, int PageSize, int PageIndex)

用途:分页功能

参数:(1)DataTable Table   —  需要分页的DataTable

     (2)int PageSize  —  页面大小

     (3)int PageIndex    —  当前页码

返回:PagePiece   —  页片实体

PagePartition

声明:PagePiece> PagePartition(IQueryable DataList, int pageSize, int pageIndex) where T : class

用途:分页功能

参数:(1)IQueryable DataList   —  需要分页的IQueryable

     (2)int PageSize  —  页面大小

     (3)int PageIndex    —  当前页码

返回:PagePiece   —  页片实体

PagePartition

声明:PagePiece> PagePartition(List DataList, int pageSize, int pageIndex) where T : class

用途:分页功能

参数:(1)List DataList   —  需要分页的List

     (2)int PageSize  —  页面大小

     (3)int PageIndex    —  当前页码

返回:PagePiece   —  页片实体

分页方法的返回值PagePiece/PagePiece<T>类,包含分页后的数据集、总页数、总数据、当前页码、等等一系列“分页”后经常会用到的数据。PagePiece/PagePiece<T>的属性的详细说明如下:

PageSize

声明:public int PageSize;

用途:int –页面大小

TotalPageNumber

声明:public int TotalPageNumber;

用途:int –总页数

TotalRecordNumber

声明:public int TotalRecordNumber;

用途:int –记录总数

CurrentStartIndex

声明:public int CurrentStartIndex;

用途:int –当前页的记录起始编号

CurrentEndIndex

声明:public int CurrentEndIndex;

用途:int –当前页的记录结束编号

CurrentPageSize

声明:public int CurrentPageSize;

用途:int –当前页的记录数量

CurrentPageIndex

声明:public int CurrentPageIndex;

用途:int –当前页码

Table

声明:public System.Data.DataTable Table;

用途:System.Data.DataTable–当前页的数据表

(或者)

DataList

声明:public List DataList;

用途:List<T>–当前页的数据

为了演示“第二种分页方案”如何使用,我们首先在Visual Studio中新建一个控制台工程。然后,我们做如下三个操作。

【第一步】:从NuGet引用DeveloperSharp包。

【第二步】:创建一个用来与数据库进行通信的“数据源类”(文本示例为:TestData.cs),内容如下:

using DeveloperSharp.Structure.Model;//DataSource的命名空间
using DeveloperSharp.Framework.QueryEngine;//DatabaseType的命名空间

namespace YZZ
{
[DataSource(DatabaseType.SQLServer, “Server=localhost;Database=Test;Uid=sa;Pwd=123”)]
public class TestData : DeveloperSharp.Structure.Model.DataLayer
{
//类中没有任何代码
}
}

说 明 :

“数据源类”(文本示例为:TestData.cs)必 须 继 承 自 DeveloperSharp.Structure.Model.DataLayer 类 , 并 且 在 其 上 设 置DataSource属 性 的 初 始 化 值 为“数据库类型”及其“链接字符串”。

【第三步】:为控制台应用类,添加通过“数据源类”(TestData)调用其PagePartition方法进行数据分页的代码。注 意:核心代码就一行而已!!

代码如下:

using DeveloperSharp.Extension;//Table扩展所在的命名空间(.NET6/VS2022用户,则需要在.csproj文件中的<ItemGroup>下添加<Using>标签)
—————————–
class Program
{
static void Main(string[] args)
{
TestData td=new TestData();

//分页
var pp=td.PagePartition(“select top 5000 * from t_Order where Id>10 order by Id desc”, 20, 162);

List<Product> Products=pp.Table.ToList<Product>();
foreach (var P in Products)
{
Console.WriteLine(P.Name);
}

Console.ReadLine();
}
}

Product类代码如下:

public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
}

此处的PagePartition方法有两个重载方法,其详细功能说明如下:

PagePartition

声明:public PagePiece PagePartition(string RecordSet, string Id, int PageSize, int PageIndex)

用途:分页功能(有主键)

参数:(1)string RecordSet     –需要分页的记录集,可以是表、视图、或者SQL语句

(2)string Id     –主键

(3)int PageSize     –页面大小

(4)int PageIndex     –当前页码

返回:PagePiece  –页片实体

PagePartition

声明:public PagePiece PagePartition(string RecordSet, int PageSize, int PageIndex)

用途:分页功能(无主键)

参数:(1)string RecordSet     — 需要分页的记录集,可以是表、视图、或者SQL语句

     (2)int PageSize    –页面大小

(3)int PageIndex    –当前页码

返回:PagePiece  –页片实体

注意:

(1)     当你需要分页的数据表有“主键”字段时,使用“分页功能(有主键)”。反之,使用“分页功能(无主键)”。

(2)     RecordSet是你需要分页的“数据总集”的SQL语句。该SQL语句的形式丰富多样,可以带条件、排序、甚至还能是多表的联合查询、等。

以上就是DeveloperSharp 高效分页使用详解的详细内容,更多关于DeveloperSharp 高效分页的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:SQL server分页的四种方法思路详解(最全面教程)SqlServer?多种分页方式?详解(含简单速度测试)浅析Mysql和Oracle分页的区别c#常用表格控件dataGridView的分页显示Spring Data JPA实现排序与分页查询超详细流程讲解MybatisPlus分页查询与多条件查询介绍及查询过程中空值问题的解决

© 版权声明

相关文章