Reputation: 15475
What's the difference between using .NextResult
and Multiple
Active Result Sets?
sqlReader.NextResult();
while (sqlReader.Read())
{
MessageBox.Show("From third SQL - " +
sqlReader.GetValue(0) + " - " +
sqlReader.GetValue(1));
}
MARS
private static string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrive it from a configuration file.
return "Data Source=(local);Integrated Security=SSPI;" +
"Initial Catalog=AdventureWorks;MultipleActiveResultSets=True";
}
Upvotes: 3
Views: 416
Reputation: 239774
They're almost opposites.
With .NextResult
, a single piece of code is consuming multiple result sets sequentially. It works through (as much of) each result set in turn by calling .Read
, and then when it no longer wants to work on that result set, it calls .NextResult
to move onto the next one. Once you've called .NextResult
, there's no way to continue consuming the previous result set.
All of these result sets must have been requested as part of the original query that was submitted to generate the DataReader
. (Either as separate SELECT
statements, or calling a stored procedure that contains such calls, or a combination of the two)
With MARS, whilst you're consuming a result set through a DataReader
, you can submit a separate query on the same connection that generates a new, separate DataReader
. Both readers may be accessed in parallel.
Upvotes: 5