My favorite way is to use a page index and page size values as input parameters to return only a subset of records from the database. Check this book out, which explains all of this in detail and more. The arguments sortexpression, pageIndex, and pageSize can come right from your GridView and hopefully to a BLL and then DAL layer to the stored procedure or SQL Text query.
For SQL Server 2000 use temporary tables.
For Sql Server 2005 you can use the ROW_NUMBER() function, which returns a consecutively numbered sequence. This sequence starts from 1 and provides a unique number for each row returned. You use the page index and page size values to calculate the lower and upper bound of the rows to return from the results of the ROW_NUMBER() function. Here is an example using a stored procedure:
CREATE PROCEDURE dbo.GetSomeItems
(
@PageIndex int,
@PageSize int
)
AS
SET NOCOUNT ON
SELECT * FROM
(
SELECT SomeItems.*,
ROW_NUMBER() OVER (ORDER BY SomeCol) AS RowNum
FROM SomeItems
) Records
WHERE Records.RowNum BETWEEN (@PageIndex*@PageSize+1) AND ((@PageIndex+1)*@PageSize)
ORDER BY Records.SomeCol
Or using a SQL Text query:
public override List<SomeItemDetails> GetSomeItems(string sortExpression, int pageIndex, int pageSize)
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
sortExpression = EnsureValidCompoundSortExpression(sortExpression);
int lowerBound = pageIndex * pageSize + 1;
int upperBound = (pageIndex + 1) * pageSize;
string sql = string.Format(@"SELECT * FROM
(
SELECT SomeItems.*,
ROW_NUMBER() OVER (ORDER BY {0}) AS RowNum
FROM SomeItems
) Records
WHERE Records.RowNum BETWEEN {1} AND {2}
ORDER BY {0}", sortExpression, lowerBound, upperBound);
SqlCommand cmd = new SqlCommand(sql, cn);
cn.Open();
return GetSomeItemCollectionFromReader(ExecuteReader(cmd));
}
}