geekth
geekth

Reputation: 43

What is the contrary to NextResult() method C#

I have two SELECT queries on a stored procedure. I found the way to go to the next result with NextResult() method but, what if I want to go to the prevoius result set?

I want to do this because I am retrieving the name of the columns using this method:

var columnNames = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();

so, for the next result set I check if there's a next result:

if (reader.NextResult())
{
  var columnNamesB = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();
}

Is there a way to do this?

Upvotes: 0

Views: 103

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100555

You can't. As most of streaming API SqlReader does not support returning to previous element. In general case what you are asking is to return back to reading some bytes from network which is just not possible.

You'll find similar behavior in many objects derived from Stream(like NetworkStream) as well as in IEnumerable.

Fix: if you need to return to previous results you either save them locally while you read it or query again.

Upvotes: 3

Related Questions