When the code reaches the loop what will be the position of the reader, is it suppose to be 0 or 1? Why does it behave this way?
bool tag= sqlDataReader.Read();
if (tag)
{
while (sqlDataReader.Read())
1 Answer 1
There is no concept of position in a DataReader
. You are always at the first position. It is forward-only. The DataReader.Read
reads the next record and returns True if a row was read, or False if no rows were read.
So, to answer your question, If your sqlDataReader
had 0 rows, tag
will be False. In such a case it won't ever enter the If
block and hence never reach the While
statement. If it had one or more rows, then tag
will be True, and your While
loop will execute until all rows have been read from the DataReader.
4 Comments
no concept of position
in my answer is that you can't move around to particular records (like go to 5th record directly etc.). Means you can't reference records by their position numbers. You can just take out only the top record from the Reader.SQLDataAdapter
to fill a DataTable
and then traverse that instead.
Read
. Remember that aDataReader
streams the records directly from the database (in reality the reader probably reads a batch of results at a time, but that doesn't change my principal message).DataTable
with aSqlDataAdapter
.