分页在前面好几节中的列子中已经出现,现在在详细讲解一下。
有三种方法去实现:
1、Top(int topCount) 方法
在sqlserver和msaccess中其实就是top , 在oracle通过rownum实现。
2、From(int startIndex, int endIndex)
从startIndex条到endIndex条之间的记录数。
3、Page(int pageSize, int pageIndex)
每页pageSize条,第pageIndex页
下面就依次对这三个方法举例说明。
top方法:
DbSession.Default.From<Products>() .Top(10) .ToList();
该查询时查询products表中的前10条记录,生成的sql语句如下:(如何输出查看组件生成的sql)
Text:
SELECT TOP 10 * FROM [Products]
DbSession.Default.From<Products>() .From(3, 8) .ToList();
查找第3条到第8条的数据(包括第3条和第8条),生成的sql语句如下:
Text:
SELECT * FROM
( SELECT TOP 6 * FROM
( SELECT TOP 8 * FROM [Products] ORDER BY [Products].[ProductID] ASC)
AS tempIntable ORDER BY [ProductID] DESC)
AS tempOuttable ORDER BY [ProductID] ASC
DbSession.Default.From<Products>() .Page(10, 2) .ToList();
Text:
SELECT * FROM
( SELECT TOP 10 * FROM
( SELECT TOP 20 * FROM [Products] ORDER BY [Products].[ProductID] ASC)
AS tempIntable ORDER BY [ProductID] DESC)
AS tempOuttable ORDER BY [ProductID] ASC
DbSession.Default.From<Products>() .Page(10, 2) .OrderBy(Products._.UnitPrice.Desc) .Where(Products._.CategoryID == 2) .ToList();
Text:这样的sql语句是不是出乎意料啊,原来符合条件的查询第二页只有2条数据,所以查询的时候就直接unitprice正序top 2就完结了。
SELECT * FROM
( SELECT TOP 2 * FROM [Products] WHERE [Products].[CategoryID] = @bee7551993404c8592f07f9b01710bb5 ORDER BY [Products].[UnitPrice] ASC)
AS temp_table ORDER BY [UnitPrice] DESC
Parameters:
@bee7551993404c8592f07f9b01710bb5[Int32] = 2
DbSession.Default.From<Products>() .Page(10, 2) .OrderBy(Products._.UnitPrice.Desc) //.Where(Products._.CategoryID == 2) .ToList();
Text:
SELECT * FROM
( SELECT TOP 10 * FROM
( SELECT TOP 20 * FROM [Products] ORDER BY [Products].[UnitPrice] DESC)
AS tempIntable ORDER BY [UnitPrice] ASC)
AS tempOuttable ORDER BY [UnitPrice] DESC
DbSession.Default.From<Products>() .Page(10, 2) .OrderBy(Products._.UnitPrice.Desc) //.Where(Products._.CategoryID == 2) .ToList();
Text:
SELECT * FROM
( SELECT *,row_number() over( ORDER BY [Products].[UnitPrice] DESC) AS tmp__rowid FROM [Products] ) AS tmp_table
WHERE (tmp__rowid BETWEEN 11 AND 20)