Close reader before closing connection

时间秒杀一切 提交于 2019-12-01 18:12:24

问题


Should you always close a DataReader before closing the connection, or is it okay to just close the connection? By just closing the connection does that effectively close the reader?

(This is for c#.net 3.5)

Thanks!


回答1:


Aside from making your intent clear, there is this from the Microsoft documentation (Retrieving Data Using a DataReader (ADO.NET)):

You should always call the Close method when you have finished using the DataReader object.




回答2:


Close the DataReader as well.

imo. if it implements IDisposable , or has a .close() method or a similar construct that the documentation states you should call when you're done with this instance - do it. Don't rely on something else to take care of it for you - unless the documentation explicitly states that it does. For a DataReader/DbConnection - the docs doesn't mention anything like this.




回答3:


I prefer to leverage the c# using statment which will call dispose() for you. Here is a decent explanation.

Using Statment



来源:https://stackoverflow.com/questions/2519379/close-reader-before-closing-connection

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