MultipleActiveResultSets=True or multiple connections?

C#.NetSql ServerSql Server-2005ado.net

C# Problem Overview


I have some C# in which I create a reader on a connection (ExecuteReader), then for every row in that reader, perform another command (with ExecuteNonQuery). In this case is it better that I use MultipleActiveResultSets=True on my connection or use multiple connections?

C# Solutions


Solution 1 - C#

Multiple Active Result Sets (MARS) was added specifically for this type of operation so that you don't have to have two connections open at the same time to be able to read from a SqlDataReader AND execute additional batches.

MARS is compatible with SQL Server 2005 and above. To quote from MSDN docs:

> Before the introduction of Multiple > Active Result Sets (MARS), developers > had to use either multiple connections > or server-side cursors to solve > certain scenarios.

For more info see:

>MSDN Library - MARS Overview

Worked example reading and updating data:

>MSDN Library - Manipulating Data (MARS) scroll down to 'Reading and Updating Data with MARS'

Solution 2 - C#

This is as far as I know the reason MARS was added, so yeah I think you should use it.

Solution 3 - C#

Best way to test this is to fire SQLServer Profiler, and see what really happens on the server side.

My guess is that it will not be better since you are using ExecuteNonQuery(). So, in fact, you don't work with multiple results.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSprintstarView Question on Stackoverflow
Solution 1 - C#KevView Answer on Stackoverflow
Solution 2 - C#Rune GrimstadView Answer on Stackoverflow
Solution 3 - C#dmajkicView Answer on Stackoverflow