ExecutePageReader Method (SqlCommand)
Returns a specific subset of rows when paging through the results of a query.
Returns a specific subset of rows when paging through the results of a query.
Parameters
- behavior
- One of the System.Data.CommandBehavior values.
- startRecord
- Specifies the number of starting record of the set of records to be returned.
- maxRecords
- Specifies total number of records to retrieve.
Return Value
A
SqlDataReader object with requested records.
The example below shows how to retrieve a part of a table. If this function is called with parameters
startRecord=2 and
maxRecords=3, it returns 3 rows: second, third, and fourth.
|
|---|
public void ExecutePaged(SqlConnection sqlConnection, int StartRow, int PageLength)
{
SqlCommand sqlCommand = new SqlCommand("SELECT * FROM Test.Dept", sqlConnection);
sqlConnection.Open();
SqlDataReader sqlReader = sqlCommand.ExecutePageReader(System.Data.CommandBehavior.Default,StartRow,PageLength);
try
{
while (sqlReader.Read())
{
Console.WriteLine(sqlReader.GetInt32(0) + ", " + sqlReader.GetString(1) + ", " + sqlReader.GetString(2));
}
}
finally
{
sqlReader.Close();
sqlConnection.Close();
}
} |
|
|---|
Public Sub ExecutePaged(ByVal sqlConnection As SqlConnection, ByVal StartRow As Integer, ByVal PageLength As Integer)
Dim sqlCommand As New SqlCommand("SELECT * FROM Test.Dept", sqlConnection)
sqlConnection.Open()
Dim sqlReader As SqlDataReader = sqlCommand.ExecutePageReader(System.Data.CommandBehavior.Default, StartRow, PageLength)
Try
While sqlReader.Read()
Console.WriteLine(sqlReader.GetInt32(0).ToString() + ", " _
+ sqlReader.GetString(1) + ", " _
+ sqlReader.GetString(2))
End While
Finally
sqlReader.Close()
sqlConnection.Close()
End Try
End Sub |