Iterate twice through a DataReader

Pascal picture Pascal · Jul 8, 2010 · Viewed 7k times · Source

I need to iterate twice through a DataReader. Is there a way to do this without using DataSets and without running the query twice? I'm using C#, by the way.
Thanks

Answer

Will Charczuk picture Will Charczuk · Jul 8, 2010

The short answer is no, you run through the enumeration for the result sets and then you're done. What should probably be done is to dump the results to some kind of simplified raw results that you can iterate over as much as you want (avoiding the overhead of the DataSet).

If you take a peek at MSDN, it notes that the SqlDataReader is "forward only" which would suggest again that this isn't possible.