Does closing a database connection in Dispose method is right?

坚强是说给别人听的谎言 提交于 2019-11-29 04:50:28

Dispose is never called automatically.

The connection will not be closed until the Dispose method of your object is explicitly called, or if your class in used in a using() block

A safer way is to call the dispose method in your finalizer and ensure the finalizer is suppressed when the Dispose method is called.

This article present the correct way to implement the pattern

Hope it helps !

Cédric

conn.Dispose(); will also close the connection, so can't hurt changing it to follow the dispose pattern.

But there functionally equivalent so there must be a problem else where.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx

If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes, the underlying connection is returned back to the connection pool. On the other hand, if Pooling is set to false or no, the underlying connection to the server is closed.

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