0

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())
asked Aug 20, 2014 at 15:08
5
  • This is very badly structured. First you should not name a variable SqlDataReader, second you simply use the While statement. It will not execute the nested statements if the reader is empty and if it contains rows then will continue until you reach the end. This is basic programming skills. Commented Aug 20, 2014 at 15:13
  • this is more or less pseudo code, naming was chosen to make it obvious for what it is... I also never said I was using this code, I am trying to understand its behaviour. This article uses the while loop so I am experimenting and learning, what is wrong with that? msdn.microsoft.com/en-us/library/… Commented Aug 20, 2014 at 15:17
  • When you create the reader via SqlCommand.ExecuteReader, the reader is not positioned on any row. You need a first call to Read and if the call is successful you are positioned on the first record. You continue your loop until the Read fails meaning there is no more records to retrieve. If you think about it this way it the most logical and requires less code. Commented Aug 20, 2014 at 15:19
  • @Bagzli: the reader does only know the current row (if any). It does not even know the previous or next. It can tell you if there is another row, but only after it tried to get it via Read. Remember that a DataReader 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). Commented Aug 20, 2014 at 15:29
  • If you need a position you could fill a DataTable with a SqlDataAdapter. Commented Aug 20, 2014 at 15:34

1 Answer 1

3

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.

answered Aug 20, 2014 at 15:18

4 Comments

According to article linked after my message, there are positions, so I am trying to understand if the position is incremented and saved when a first check is implemented. "The default position of the SqlDataReader is before the first record. Therefore, you must call Read to begin accessing any data." msdn.microsoft.com/en-us/library/…
Where the Reader is up to in the data set is irrelevant - it's either on a record or it's not.
@Bagzli, What I meant by 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.
As an addendum, if you do want random access to the results of your query, use a SQLDataAdapter to fill a DataTable and then traverse that instead.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.