Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
    }
}
like image 730
Aan Avatar asked May 03 '13 14:05

Aan


3 Answers

You can't.

The *DataReader classes are forward-only iterators.

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

like image 137
SLaks Avatar answered Oct 15 '22 02:10

SLaks


The only way to restart it is to grab a new reader with ExecuteReader().

like image 6
Andomar Avatar answered Oct 15 '22 02:10

Andomar


You can do that by first closing the datareader using dr.close(); then initializing it again.

If(condition)
{
    dr.close();
    dr=command.ExecuteReader();
}

Where command is the MySqlCommand object.

like image 6
danish_wani Avatar answered Oct 15 '22 02:10

danish_wani