ADO.NET 101: SqlDataReader - SQLDataReader Class
(Page 2 of 7 )
The SQLDataReader object (as well as the other analogous ODBCDataReader, OleDbDataReader and OracleDataReader objects) does not have a GUI, but is accessed via SQLCommand. SqlDataReader is used to retrieve read-only, forward-only, row after row of streaming data from the SQL Server. Since the DataReader is connected to the server, it should be closed after retrieving the necessary data. While the data is streaming into the SQLDataReader, the connection is busy and cannot be used for other purposes. DataReader is especially useful if fast access is needed. As it retrieves, there will be only one row in the memory, reducing system ovehead.
SQLDataReader is a non-inheritable class, and this next screen shot shows the Class Viewer showing the details of the SQLDataReader class. It has a few properties and a large number of methods as shown in this class view. For best performance use the various Get.... methods of DataReader. If the column datatype is known before hand, using these typed accessor methods will improve performance because no data conversion is involved in retrieval. As will be seen presently, column or field access can be both by name and ordinal reference.

The properties of the SQLDataReader can be utilized to find the number of columns of data retrieved, whether or not any rows are returned by a command, and whether the reader is closed, or open, as shown in this tutorial. Very simply stated, in order to work with SQLDataReader, follow this recipe:
- Establish a SqlConnection and open the connection
- Create a SQLCommand
- Use one of the execute methods of the SqlCommand to enable the reader
- Optionally monitor SQLDataReader's Properties
- Read the contents of the SQLDataReader
- Close the SqlDataReader
- Close the SqlConnection
Next: SQLCommand Execution >>
More Database Articles
More By Jayaram Krishnaswamy