MultipleActiveResultSet = 真或多个连接?

我有一些 C # ,其中我在一个连接上创建了一个读取器(ExecuteReader) ,然后对该读取器中的每一行执行另一个命令(使用 ExecuteNonQuery)。在这种情况下,在连接上使用 MultipleActiveResultSets=True或使用多个连接是否更好?

89206 次浏览

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

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.

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'