For implementing a custom paging solution using C#, ADO.Net
, with SQL Server as database, let us assume following details:
Total Records = 1000
Page Size = 100
This would mean there are 10 pages
, I use a query as follows:
Min and Max Id (Primary Key)
are fetched using scalar Queries:
Select Min(Id), Max(Id) from Table;
Select * from Table where Id >= (Min Record) and Id < (Max Record)
Here for every transaction, Min and Max Record Value
is incremented by Page Size, till the point Max Record reach or exceed the Actual Max value.
This solution works fine for an auto increment / identity column, which have continuous values, but let's assume the column used has gap in values, though they can still be ordered, like 1000 values
are between Min = 1 and Max = 3000
.
I have following possible work around, one suggested in following SO link, where an extra
RowId column
is used to achieve the same.Other options would be opening a Reader and read a record for given Page, create a List and thus fetch a Page
What I ideally want is executing a Top <PageSize>
sql query, where it is executed from a pre-defined row or value that I suggest, instead of beginning, thus I would avoid creating an extra column, I can work with disconnected architecture, no Reader is required.
Any suggestion or pointer, please let me know if a clarification is required, for any detail in the question