SqlDataReader 内部的 SqlDataReader
如何在另一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要有两个嵌套的数据读取器,这需要 ADO.NET“MARS”功能 - 多个活动结果集。
从 ADO.NET 2.0 开始可用,并且需要在连接字符串中进行特定设置 (
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: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 sameSqlConnection
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....
您遇到的问题是您尝试针对同一数据库连接打开多个数据读取器。默认情况下,这会给你一个例外,因为它会说已经有一个与连接关联 - 正如你所看到的。
您可以使用 MultipleActiveResultsets,从而从 . NET 2.0 和从 SQL Server 2005 开始,您可以在连接字符串中指定一个额外选项,通过添加以下内容来针对单个数据库连接启用多个活动结果集:
另一种方法是使用不同的连接来打开内部数据读取器。
或者甚至,可以重新考虑您原来的方法 - 也许有一种方法可以在没有嵌套读者的情况下做到这一点。
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:
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.