开发者

Close reader before closing connection

开发者 https://www.devze.com 2022-12-24 00:16 出处:网络
Should you always close a DataReader before closing the connection, or 开发者_Go百科is it okay to just close the connection? By just closing the connection does that effectively close the reader?

Should you always close a DataReader before closing the connection, or 开发者_Go百科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!


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.


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.


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

Using Statment

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号