使用记录中有间隙的数据列进行自定义分页
Custom paging using Data Column with gap in Records
为了使用 C#, ADO.Net
实现自定义分页解决方案,将 SQL 服务器作为数据库,让我们假设以下细节:
Total Records = 1000
Page Size = 100
这意味着有10 pages
,我使用如下查询:
Min and Max Id (Primary Key)
使用标量查询获取:
Select Min(Id), Max(Id) from Table;
Select * from Table where Id >= (Min Record) and Id < (Max Record)
此处对于每个事务,Min and Max Record Value
增加页面大小,直到最大记录点达到或超过实际最大值。
此解决方案适用于具有连续值的自动递增/标识列,但我们假设所使用的列在值上存在差距,尽管它们仍然可以排序,例如 1000 values
介于 Min = 1 and Max = 3000
.
我有以下可能的解决方法,在 SO link 中提出了一个建议,其中使用额外的 RowId column
来实现相同的目的。
其他选项是打开一个 Reader 并读取给定页面的记录,创建一个列表,从而获取一个页面
我最理想的是执行一个 Top <PageSize>
sql 查询,它是从我建议的预定义行或值开始执行的,而不是从头开始,因此我会避免创建一个额外的专栏,我可以使用断开连接的架构,不需要 Reader。
任何建议或指示,如果需要澄清,请让我知道问题中的任何细节
您可以使用 ROW_NUMBER
function in a common table expression(CTE):
WITH CTE AS
(
SELECT
t.*,
RN = ROW_NUMBER() OVER (ORDER BY t.ID)
FROM dbo.TableName t
)
SELECT ID, Col2, Col3, ...
FROM CTE
WHERE RN >= (@pageIndedx * @pageSize) AND RN <= (@pageIndedx * @pageSize) + @pageSize
如果您使用的是 Sql Server 2012 以上版本,您可以使用
Fetch and Offset
例如
-- Variable to hold the offset value
Declare @RowSkip As int
-- Variable to hold the fetch value
Declare @RowFetch As int
--Set the value of rows to skip
Set @RowSkip = 20000
--Set the value of rows to fetch
Set @RowFetch = 50000
Select *
From dbo.tblSample
Order by (Select 1)
Offset @RowSkip Row
Fetch Next @RowFetch Rows Only;
为了使用 C#, ADO.Net
实现自定义分页解决方案,将 SQL 服务器作为数据库,让我们假设以下细节:
Total Records = 1000
Page Size = 100
这意味着有10 pages
,我使用如下查询:
Min and Max Id (Primary Key)
使用标量查询获取:
Select Min(Id), Max(Id) from Table;
Select * from Table where Id >= (Min Record) and Id < (Max Record)
此处对于每个事务,Min and Max Record Value
增加页面大小,直到最大记录点达到或超过实际最大值。
此解决方案适用于具有连续值的自动递增/标识列,但我们假设所使用的列在值上存在差距,尽管它们仍然可以排序,例如 1000 values
介于 Min = 1 and Max = 3000
.
我有以下可能的解决方法,在 SO link 中提出了一个建议,其中使用额外的
RowId column
来实现相同的目的。其他选项是打开一个 Reader 并读取给定页面的记录,创建一个列表,从而获取一个页面
我最理想的是执行一个 Top <PageSize>
sql 查询,它是从我建议的预定义行或值开始执行的,而不是从头开始,因此我会避免创建一个额外的专栏,我可以使用断开连接的架构,不需要 Reader。
任何建议或指示,如果需要澄清,请让我知道问题中的任何细节
您可以使用 ROW_NUMBER
function in a common table expression(CTE):
WITH CTE AS
(
SELECT
t.*,
RN = ROW_NUMBER() OVER (ORDER BY t.ID)
FROM dbo.TableName t
)
SELECT ID, Col2, Col3, ...
FROM CTE
WHERE RN >= (@pageIndedx * @pageSize) AND RN <= (@pageIndedx * @pageSize) + @pageSize
如果您使用的是 Sql Server 2012 以上版本,您可以使用
Fetch and Offset
例如
-- Variable to hold the offset value
Declare @RowSkip As int
-- Variable to hold the fetch value
Declare @RowFetch As int
--Set the value of rows to skip
Set @RowSkip = 20000
--Set the value of rows to fetch
Set @RowFetch = 50000
Select *
From dbo.tblSample
Order by (Select 1)
Offset @RowSkip Row
Fetch Next @RowFetch Rows Only;