System.Data.SqlClient.SqlDataReader Class

Provides a way of reading a forward-only stream of rows from a SQL Server database. This class cannot be inherited.

See Also: SqlDataReader Members


public class SqlDataReader : System.Data.Common.DbDataReader


To create a System.Data.SqlClient.SqlDataReader, you must call the SqlCommand.ExecuteReader method of the System.Data.SqlClient.SqlCommand object, instead of directly using a constructor.

While the System.Data.SqlClient.SqlDataReader is being used, the associated System.Data.SqlClient.SqlConnection is busy serving the System.Data.SqlClient.SqlDataReader, and no other operations can be performed on the System.Data.SqlClient.SqlConnection other than closing it. This is the case until the SqlDataReader.Close method of the System.Data.SqlClient.SqlDataReader is called. For example, you cannot retrieve output parameters until after you call SqlDataReader.Close.

Changes made to a result set by another process or thread while data is being read may be visible to the user of the SqlDataReader. However, the precise behavior is timing dependent.

SqlDataReader.IsClosed and SqlDataReader.RecordsAffected are the only properties that you can call after the System.Data.SqlClient.SqlDataReader is closed. Although the SqlDataReader.RecordsAffected property may be accessed while the System.Data.SqlClient.SqlDataReader exists, always call SqlDataReader.Close before returning the value of SqlDataReader.RecordsAffected to guarantee an accurate return value.

When using sequential access (mmandBehavior.SequentialAccess), an InvalidOperationException will be raised if the System.Data.SqlClient.SqlDataReader position is advanced and another read operation is attempted on the previous column.


For optimal performance, System.Data.SqlClient.SqlDataReader avoids creating unnecessary objects or making unnecessary copies of data. Therefore, multiple calls to methods such as SqlDataReader.GetValue(int) return a reference to the same object. Use caution if you are modifying the underlying value of the objects returned by methods such as SqlDataReader.GetValue(int).


Namespace: System.Data.SqlClient
Assembly: System.Data (in System.Data.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0,