Bagzli
Bagzli

Reputation: 6577

SqlDataReader poisition

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())

Upvotes: 0

Views: 215

Answers (1)

Pradeep Kumar
Pradeep Kumar

Reputation: 6979

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.

Upvotes: 3

Related Questions