DontVoteMeDown
DontVoteMeDown

Reputation: 21465

DataReader cursor rewind

How do I can rewind a cursor of a DataReader to the beginning?

With one DataReader result I need to run two while loop, but those have to be from beginning. They are two iterations in one result set running a query once.

Example:

dr = command.ExecuteReader(cmd);

while (dr.Read()) { 
    // do some...
}

// rewind cursor here

while (dr.Read()) {
    // do another things...
}

I've looked into the DataReader docs and I've found nothing, so if it can't be possible with DataReader, I may change the class for one that fits this purpose.

Upvotes: 4

Views: 5649

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062865

You cannot (unless you execute the command again): it is a one-way stream. If you want to see the data more than once you'll have to buffer it in memory yourself, for example in a List<T> (for some T), or (yeuch) as a DataTable.

Upvotes: 3

Related Questions