C# MultipleActiveResultSets=True 还是多个连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/510899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:14:51  来源:igfitidea点击:

MultipleActiveResultSets=True or multiple connections?

c#.netsql-serversql-server-2005ado.net

提问by Sprintstar

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=Trueon my connection or use multiple connections?

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

采纳答案by Kev

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) 是专门为此类操作添加的,因此您不必同时打开两个连接即可从 SqlDataReader 读取数据并执行额外的批处理。

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

MARS 与 SQL Server 2005 及更高版本兼容。引用 MSDN 文档:

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

在引入多活动结果集 (MARS) 之前,开发人员必须使用多个连接或服务器端游标来解决某些场景。

For more info see:

有关更多信息,请参阅:

MSDN Library - MARS Overview

MSDN 库 - MARS 概述

Worked example reading and updating data:

读取和更新数据的工作示例:

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

MSDN 库 - 操作数据 (MARS)向下滚动到“使用 MARS 读取和更新数据”

回答by Rune Grimstad

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

据我所知,这是添加 MARS 的原因,所以是的,我认为您应该使用它。

回答by dmajkic

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

测试此问题的最佳方法是启动 SQLServer Profiler,然后查看服务器端实际发生的情况。

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

我的猜测是它不会更好,因为您使用的是 ExecuteNonQuery()。因此,实际上,您不会处理多个结果。