VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • .NET中大型项目开发必备(8)–高效分页

相关下载:

DeveloperSharp.dll组件

http://www.365ey.net/article/DeveloperSharp.dll

DeveloperSharp.dll组件

https://files-cdn.cnblogs.com/files/blogs/721296/(2022)DeveloperSharp.rar

高效分页-示例代码(dp8-Paging.rar)

http://www.365ey.net/article/dp8-Paging.rar

高效分页-示例代码(dp8-Paging.rar)

https://files-cdn.cnblogs.com/files/blogs/721296/(2022)dp8-Paging.rar
 

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

 

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

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

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

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

 

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

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

 

◆◆第一种方案如下◆◆

引入DeveloperSharp.dll组件,初始化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
     (2int PageSize  --  页面大小
     (3int PageIndex    --  当前页码(最小值为1)
返回:PagePiece   --  页片实体

PagePartition
声明:PagePiece<List<T>> PagePartition<T>(IQueryable<T> DataList, int pageSize, int pageIndex) where T : class
用途:分页功能
参数:(1)IQueryable<T> DataList   --  需要分页的IQueryable
     (2int PageSize  --  页面大小
     (3int PageIndex    --  当前页码(最小值为1)
返回:PagePiece   --  页片实体

PagePartition
声明:PagePiece<List<T>> PagePartition<T>(List<T> DataList, int pageSize, int pageIndex) where T : class
用途:分页功能
参数:(1)List<T> DataList   --  需要分页的List
     (2int PageSize  --  页面大小
     (3int PageIndex    --  当前页码(最小值为1)
返回: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<T> DataList;
用途:List<T>--当前页的数据
复制代码

 

 

◆◆第二种方案如下◆◆

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

【第一步】:为工程引用添加 DeveloperSharp.dll组件。

【第二步】:创建一个用来与数据库进行通信的“数据源类”(文本示例为: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.Structure.Model;//PagePiece所在的命名空间
using DeveloperSharp.Extension;//Table扩展所在的命名空间
-----------------------------
    class Program
    {
        static void Main(string[] args)
        {
            TestData td = new TestData();

            //分页
            PagePiece pp = td.PagePartition("select top 500000 * 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)
用途:分页功能(有主键)
参数:(1string RecordSet     --需要分页的记录集,可以是表、视图、或者SQL语句
(2string Id     --主键
(3int PageSize     --页面大小
(4int PageIndex     --当前页码(最小值为1)
返回:PagePiece  --页片实体

PagePartition
声明:public PagePiece PagePartition(string RecordSet, int PageSize, int PageIndex)
用途:分页功能(无主键)
参数:(1string RecordSet     -- 需要分页的记录集,可以是表、视图、或者SQL语句
     (2int PageSize    --页面大小
(3int PageIndex    --当前页码(最小值为1)
返回:PagePiece  --页片实体
复制代码

注意:

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

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

出  处:https://www.cnblogs.com/DeveloperSharp/p/15824839.html


相关教程