• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

VB.Net - DataReader Looping

Good Day Experts!

What is your preference to looping through a DataReader?  

I have seen:

While reader.Read()

End While

**************************

With reader
     .Read()
     Do While .HasRows

     Loop
End With

What are the pros/cons if any between the 2?

Thanks,
jimbo99999
0
Jimbo99999
Asked:
Jimbo99999
2 Solutions
 
CodeCruiserCommented:
I don't think there is any difference between the two approaches. Infact I suspect #2 will throw an error if there are no rows. I prefer the #1 approach.
0
 
Jacques Bourgeois (James Burger)Commented:
CodeCruiser has it, there is not difference, but you should use the first set condition if you want to go the Loop route: Do While .Read().

There is something of a was between the proponents of the While and those who would rather use the Loop.

There is only one form of While...End While.

But there are 4 variations of the Do...Loop:
    Do While...Loop
    Do...Loop While
    Do Until...Loop
    Do...Loop Until

Those who prefer the Do...Loop argue that it gives you more control. I disagree with that. What I have seen in many years of code revision and debugging other programmers code is that those who use it often make mistakes. They think that they are in one form while they are in the other. This often end up missing one loop or doing one too many.

There is nothing that you can do with the Do...Loop that you cannot do with the While...End While. Just adjust the expression accordingly. But in my opinion, programmers who use the While...End While have only one way of seeing that kind of loop. They are used to it, and they do less mistakes.
0
 
Jimbo99999Author Commented:
Thanks for the feedback as I continue my journey.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now