19

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
    }
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Aan
  • 12,247
  • 36
  • 89
  • 150

3 Answers3

23

You can't.

The *DataReader classes are forward-only iterators.

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
6

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

Andomar
  • 232,371
  • 49
  • 380
  • 404
6

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.

CDspace
  • 2,639
  • 18
  • 30
  • 36
danish_wani
  • 862
  • 9
  • 14