Make DbDataReader start reading again from the beginning of the result set

Aan picture Aan · May 3, 2013 · Viewed 19.6k times · Source

How to make dr.Read(); start reading again from the beginning if a condition is satisfied?

Something like:

SqlDataReader dr = command.ExecuteReader();
for(int i=0; dr.Read() ; i++){
    if(condition ){
        //let dr.Read() start reading from the beginning
    }
}

Answer

SLaks picture SLaks · May 3, 2013

You can't.

The *DataReader classes are forward-only iterators.

Instead, you can store the results in a List<T> (or a DataTable)