SqlDataReader:在这种情况下,读取器会被关闭吗?

发布于 2024-07-08 06:10:42 字数 650 浏览 7 评论 0原文

我正在清理我继承的旧 .NET 1.1 项目中的 DataReader。

以前的开发人员以大多数 DAL 方法返回 SqlDataReaders 的方式对数据访问层进行编码(因此由调用者正确调用 .Close() 或 .Dispose() 方法)。

不过,我遇到过一种情况,调用者没有捕获返回的 SqlDataReader (因此没有正确处理它)。 请参阅下面的代码:

数据访问方法:

Public Shared Function UpdateData() As SqlDataReader
   ...

   drSQL = cmdSQL.ExecuteReader(CommandBehavior.CloseConnection)

   Return drSQL

End Function

调用代码:

...
DataAccessLayer.UpdateData()
...

正如您所看到的,调用方法不会接收/捕获返回的 SqlDataReader。 那么会发生什么呢? SqlDataReader 仍然存在并打开吗? 或者它会自动收集垃圾,因为没有任何东西可以解决它?

我想不出一种方法来调试和测试它。 如果有人有任何想法或建议,那就太好了。

I am cleaning up the DataReaders in an old .NET 1.1 project that I inherited.

The previous developer coded the data-access-layer in such a way that most of the DAL methods returned SqlDataReaders (thus leaving it up to the caller to properly call the .Close() or .Dispose() methods).

I have come across a situation, though, where a caller is not catching the returned SqlDataReader (and therefore is not disposing of it properly). See the code below:

Data Access Method:

Public Shared Function UpdateData() As SqlDataReader
   ...

   drSQL = cmdSQL.ExecuteReader(CommandBehavior.CloseConnection)

   Return drSQL

End Function

Calling code:

...
DataAccessLayer.UpdateData()
...

As you can see, the calling method does not receive/catch the returned SqlDataReader. So what happens? Is that SqlDataReader still out there and open? Or does it automatically get garbage collected since nothing is addressing it?

I couldn't think of a way to debug and test this. If anybody has any ideas or suggestions that would be great.

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

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

发布评论

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

评论(1

爱本泡沫多脆弱 2024-07-15 06:10:42

我相信它会被关闭,但直到垃圾收集器开始处理它,这可能不会持续很长时间......

i believe that it will get closed but not until the garbage-collector gets 'round to it, which may not be for a very long time...

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