DeveloperSharp?高效分頁使用詳解
正文
支持.Net/.Net Core/.Net Framework,可以部署在Docker, Windows, Linux, Mac。
數(shù)據(jù)分頁,幾乎是任何應(yīng)用系統(tǒng)的必備功能。但當數(shù)據(jù)量較大時,分頁操作的效率就會變得很低。大數(shù)據(jù)量分頁時,一個操作耗時5秒、10秒、甚至更長時間都是有可能的,但這在用戶使用的角度是不可接受的……
數(shù)據(jù)分頁往往有三種常用方案。
第一種,把數(shù)據(jù)庫中存放的相關(guān)數(shù)據(jù),全部讀入代碼/內(nèi)存,再由代碼對其進行分頁操作。
第二種,直接在數(shù)據(jù)庫中對相關(guān)數(shù)據(jù)進行分頁操作,再把分頁后的數(shù)據(jù)輸出給代碼程序。
第三種,先把數(shù)據(jù)庫中的相關(guān)數(shù)據(jù)全部讀入“緩存”,再由代碼程序?qū)?ldquo;緩存”中的數(shù)據(jù)進行讀取+分頁操作。
本文下面重點闡述第一種與第二種兩個解決方案,它們也都是直接基于“數(shù)據(jù)庫”的。
(第三種方案雖然速度較快,但由于需要用到“緩存”這類第三方工具,且在有數(shù)據(jù)更改時需要較復(fù)雜的“數(shù)據(jù)庫-緩存”同步操作,故本文暫不詳述。)
◆◆第一種方案如下◆◆
從NuGet引入DeveloperSharp包,初始化IUtility工具
using DeveloperSharp.Framework.CoreUtility; -------------------------- IUtility IU = new Utility();
IUtility內(nèi)構(gòu)建有3個重載的分頁PagePartition方法:
PagePartition
聲明:PagePiece PagePartition(DataTable Table, int PageSize, int PageIndex)
用途:分頁功能
參數(shù):(1)DataTable Table -- 需要分頁的DataTable
(2)int PageSize -- 頁面大小
(3)int PageIndex -- 當前頁碼
返回:PagePiece -- 頁片實體
PagePartition
聲明:PagePiece<List<T>> PagePartition<T>(IQueryable<T> DataList, int pageSize, int pageIndex) where T : class
用途:分頁功能
參數(shù):(1)IQueryable<T> DataList -- 需要分頁的IQueryable
(2)int PageSize -- 頁面大小
(3)int PageIndex -- 當前頁碼
返回:PagePiece -- 頁片實體
PagePartition
聲明:PagePiece<List<T>> PagePartition<T>(List<T> DataList, int pageSize, int pageIndex) where T : class
用途:分頁功能
參數(shù):(1)List<T> DataList -- 需要分頁的List
(2)int PageSize -- 頁面大小
(3)int PageIndex -- 當前頁碼
返回:PagePiece -- 頁片實體
分頁方法的返回值PagePiece/PagePiece<T>類,包含分頁后的數(shù)據(jù)集、總頁數(shù)、總數(shù)據(jù)、當前頁碼、等等一系列“分頁”后經(jīng)常會用到的數(shù)據(jù)。PagePiece/PagePiece<T>的屬性的詳細說明如下:
PageSize
聲明:public int PageSize;
用途:int --頁面大小
TotalPageNumber
聲明:public int TotalPageNumber;
用途:int --總頁數(shù)
TotalRecordNumber
聲明:public int TotalRecordNumber;
用途:int --記錄總數(shù)
CurrentStartIndex
聲明:public int CurrentStartIndex;
用途:int --當前頁的記錄起始編號
CurrentEndIndex
聲明:public int CurrentEndIndex;
用途:int --當前頁的記錄結(jié)束編號
CurrentPageSize
聲明:public int CurrentPageSize;
用途:int --當前頁的記錄數(shù)量
CurrentPageIndex
聲明:public int CurrentPageIndex;
用途:int --當前頁碼
Table
聲明:public System.Data.DataTable Table;
用途:System.Data.DataTable--當前頁的數(shù)據(jù)表
(或者)
DataList
聲明:public List<T> DataList;
用途:List<T>--當前頁的數(shù)據(jù)
◆◆第二種方案如下◆◆
為了演示“第二種分頁方案”如何使用,我們首先在Visual Studio中新建一個控制臺工程。然后,我們做如下三個操作。
【第一步】:從NuGet引用DeveloperSharp包。
【第二步】:創(chuàng)建一個用來與數(shù)據(jù)庫進行通信的“數(shù)據(jù)源類”(文本示例為:TestData.cs),內(nèi)容如下:
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 { //類中沒有任何代碼 } }
說 明 :
“數(shù)據(jù)源類”(文本示例為:TestData.cs)必 須 繼 承 自 DeveloperSharp.Structure.Model.DataLayer 類 , 并 且 在 其 上 設(shè) 置DataSource屬 性 的 初 始 化 值 為“數(shù)據(jù)庫類型”及其“鏈接字符串”。
【第三步】:為控制臺應(yīng)用類,添加通過“數(shù)據(jù)源類”(TestData)調(diào)用其PagePartition方法進行數(shù)據(jù)分頁的代碼。注 意:核心代碼就一行而已?。?/p>
代碼如下:
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)
用途:分頁功能(有主鍵)
參數(shù):(1)string RecordSet --需要分頁的記錄集,可以是表、視圖、或者SQL語句
(2)string Id --主鍵
(3)int PageSize --頁面大小
(4)int PageIndex --當前頁碼
返回:PagePiece --頁片實體
PagePartition
聲明:public PagePiece PagePartition(string RecordSet, int PageSize, int PageIndex)
用途:分頁功能(無主鍵)
參數(shù):(1)string RecordSet -- 需要分頁的記錄集,可以是表、視圖、或者SQL語句
(2)int PageSize --頁面大小
(3)int PageIndex --當前頁碼
返回:PagePiece --頁片實體
注意:
(1) 當你需要分頁的數(shù)據(jù)表有“主鍵”字段時,使用“分頁功能(有主鍵)”。反之,使用“分頁功能(無主鍵)”。
(2) RecordSet是你需要分頁的“數(shù)據(jù)總集”的SQL語句。該SQL語句的形式豐富多樣,可以帶條件、排序、甚至還能是多表的聯(lián)合查詢、等。
以上就是DeveloperSharp 高效分頁使用詳解的詳細內(nèi)容,更多關(guān)于DeveloperSharp 高效分頁的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
通過Qt連接OpenGauss數(shù)據(jù)庫的詳細教程
本教程介紹如何通過Qt連接OpenGauss數(shù)據(jù)庫,在openGauss所在的root環(huán)境下執(zhí)行相關(guān)步驟,需要Windows下配置ODBC數(shù)據(jù)源,本文給大家介紹的非常詳細,需要的朋友參考下吧2021-06-06圖文詳解如何在navicat中導(dǎo)入excel表格數(shù)據(jù)
Navicat可以方便的操作各種數(shù)據(jù)庫,也提供了豐富的導(dǎo)入導(dǎo)出功能,下面這篇文章主要給大家介紹了關(guān)于如何在navicat中導(dǎo)入excel表格數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2023-02-02使用DataGrip創(chuàng)建數(shù)據(jù)庫并讀取sql文件圖文教程
這篇文章主要給大家介紹了關(guān)于使用DataGrip創(chuàng)建數(shù)據(jù)庫并讀取sql文件的相關(guān)資料,DataGrip是一款數(shù)據(jù)庫管理客戶端工具,方便連接到數(shù)據(jù)庫服務(wù)器,執(zhí)行sql、創(chuàng)建表、創(chuàng)建索引以及導(dǎo)出數(shù)據(jù)等,需要的朋友可以參考下2023-11-11數(shù)據(jù)庫中的左連接(left join)和右連接(right join)區(qū)別
關(guān)于左連接和右連接總結(jié)性的一句話,左連接 where只影向右表,右連接where只影響左表2012-06-06