Data Reader
An alternative to creating a DataSet is to create an instance of DataReader. The DataReader provides connected, forward-only, read-only access to a collection of tables, by, for example, executing a SQL statement or a stored procedure. DataReaders are lightweight objects ideally suited for filling a control or a form with data and then breaking the connection to the backend database.
Tip
Like DataAdapter, the DataReader class comes in two flavors: SqlDataReader for use with SQL Server and OleDbDataReader for use with other databases.
Table 19-5 shows the most important methods and properties of the DataReader class.
Table 19-5. The most important DataReader methods
Class member |
Description |
---|---|
Close |
Closes the DataReader. |
NextResult |
When reading the results of a batch SQL statement, advances to the next result set (set of records). |
Read |
Read a record and advance the iterator. Returns
|
The DataReader is a very powerful object, but you won't use many of its methods or properties often. Most of the time, you'll simply use the DataReader to retrieve and iterate through the records that represent the result of your query.
Tip
Note to ADO programmers: you do not issue a MoveNext command to the DataReader. By reading a record, you automatically move to the next record. This eliminates one of the most common bugs with recordsets — forgetting to move to the next record.
Do not try to create a DataReader by instantiating it ...
Get Programming .NET Windows Applications now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.