SqlDataReader: In this scenario, will the reader get closed?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 03:48:15

问题


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.


回答1:


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...



来源:https://stackoverflow.com/questions/247311/sqldatareader-in-this-scenario-will-the-reader-get-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!