A SqlDataReader is good for reading data in the most efficient manner. We can not use it for writing data. We can read from
SqlDataReader objects in a forward-only sequential manner. Once we have read some data, we must save it because we will not be able to go back and read it again.
The forward only design of the SqlDataReader is what enables it to be fast. It doesn't have overhead associated with traversing the data or writing it back to the data source. Therefore, if our only requirement for a group of data is for reading one time and we want the fastest method possible, the
SqlDataReader is the best choice. Also, if the amount of data we need to read is larger than what we would prefer to hold in memory beyond a single call, then the streaming behavior of the
SqlDataReader would be a good choice.
Creating a SqlDataReader Object
rdr = cmd.ExecuteReader();
The ExecuteReadermethod of the
SqlCommand object, cmd , returns a
The SqlDataReader returns data via a sequential stream. To read this data, we must extract data from a table row-by-row. Once a row has been read, the previous row is no longer available. To read that row again, we would have to create a new instance of the
SqlDataReader and read through the data stream again.