SqlDataReader 内部的 SqlDataReader

发布于 2024-08-23 12:10:08 字数 501 浏览 2 评论 0原文

如何在另一个 SqlDataReader 中实现一个 SqlDataReader

我的问题是我有一个 SqlDataReader。我正在发出 while (reader.read()) ,并且在 while 循环内我必须创建另一个 SqlDataReader 来从数据库中读取数据。但我收到有关连接已打开的异常。

那么解决我的问题的最佳方法是什么?

编辑:

我正在使用 clr 创建存储过程。我尝试将 MultipleActiveResultSets=true; 放入 clr 和项目的连接字符串中,当我 在 SQL Server 上测试我的存储过程:

System.InvalidOperationException:已经有一个与此命令关联的打开的 DataReader,必须先将其关闭。

How can I implement a SqlDataReader inside another SqlDataReader?

My problem is I have a SqlDataReader. I am issuing while (reader.read()) and inside the while loop I have to create another SqlDataReader to read from the database. But I'm getting exceptions about connection being already open.

So what's the best way to solve my problem?

Edit:

I am using clr to create my stored procedures. I tried to put MultipleActiveResultSets=true; within the connection string of both the clr and the project, and an exception occurred when I
tested my stored procedure on SQL Server:

System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野生奥特曼 2024-08-30 12:10:08

您需要有两个嵌套的数据读取器,这需要 ADO.NET“MARS”功能 - 多个活动结果集。

从 ADO.NET 2.0 开始可用,并且需要在连接字符串中进行特定设置 (MultipleActiveResultSets=true;):

Server=.\SQLEXPRESS;Database=master;Integrated Security=SSPI;
  MultipleActiveResultSets=true;

请参阅此 博客文章 进行精彩的讨论。

一旦有了这个,您应该能够在代码中的同一个 SqlConnection 上共享多个 SqlDataReader,并相互独立地使用它们。

更新:此处的博客文章 提到 MARS 功能在 SQL CLR 环境中不可用:-( 所以这在 SQL CLR 存储过程中不起作用......

You need to have two nested data readers, and this requires the ADO.NET "MARS" feature - Multiple Active Result Sets.

This is available as of ADO.NET 2.0, and requires a specific setting (MultipleActiveResultSets=true;) in the connection string:

Server=.\SQLEXPRESS;Database=master;Integrated Security=SSPI;
  MultipleActiveResultSets=true;

See this blog post for an excellent discussion.

Once you have this, you should be able to have more than one SqlDataReader shared on the same SqlConnection in your code, and use them independently of one another.

UPDATE: this blog post here mentions that the MARS feature is not available inside the SQL CLR environment :-( So that won't work inside a SQL CLR stored proc....

行至春深 2024-08-30 12:10:08

您遇到的问题是您尝试针对同一数据库连接打开多个数据读取器。默认情况下,这会给你一个例外,因为它会说已经有一个与连接关联 - 正如你所看到的。

您可以使用 MultipleActiveResultsets,从而从 . NET 2.0 和从 SQL Server 2005 开始,您可以在连接字符串中指定一个额外选项,通过添加以下内容来针对单个数据库连接启用多个活动结果集:

MultipleActiveResultSets=True;

另一种方法是使用不同的连接来打开内部数据读取器。

或者甚至,可以重新考虑您原来的方法 - 也许有一种方法可以在没有嵌套读者的情况下做到这一点。

The issue you're encountering is that you are trying to open multiple data readers against the same db connection. By default, this will give you an exception as it will say there's already one associated with the connection - as you've seen.

You could make use of MultipleActiveResultsets, whereby from .NET 2.0 & SQL Server 2005 onwards, you can specify an extra option in the connection string to enable multiple active resultsets against a single db connection by adding:

MultipleActiveResultSets=True;

The alternative is to use a different connection to open the inner data reader against.

Or even, it may be possible to rethink your original approach - maybe there's a way to do it without nested readers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文