Aan
Aan

Reputation: 12890

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
    }
}

Upvotes: 20

Views: 24246

Answers (3)

danish_wani
danish_wani

Reputation: 872

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.

Upvotes: 6

SLaks
SLaks

Reputation: 887459

You can't.

The *DataReader classes are forward-only iterators.

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

Upvotes: 26

Andomar
Andomar

Reputation: 238086

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

Upvotes: 6

Related Questions