Does OledbConnection.Dispose() close the connection? [duplicate]

隐身守侯 提交于 2019-12-07 02:32:25

问题


Possible Duplicate:
Is there any need to close a DbConnection if a using clause is used?

Does OledbConnection.Dispose() close the connection?

I am aware that SqlConnection does but what about OledbConnection?


回答1:


Yes, it also does.

Source: OleDbConnection.Dispose Method (Boolean)

The Dispose method calls Close, and removes the OleDbConnection from the connection pool.

For detailed information see the Remarks section on the reference link to know about the case of releasing both managed and unmanaged resources.




回答2:


Yes, according to the documentation on MSDN http://msdn.microsoft.com/en-us/library/aa325890(v=vs.71).aspx , OleDbConnection.Dispose() also does call the OleDbConnection.Close().




回答3:


Yes it does MSDN:

The Dispose method calls Close, and removes the OleDbConnection from the connection pool.

Note, that the above is from the .NET Framework 1.1. But (in this case) you can count on things having not changed.

Also, you can be almost 100% sure that every class that implements IDbConnection will "close" the connection in the Dispose method - whatever that means for the particular implementation is not relevant, but it will be equivalent to calling Close manually.

Every implementation that does not behave that way, should be IMO considered broken.




回答4:


Here is the ultimate proof.. the actual code of the Dispose method, taken using a reflector:

// System.Data.OleDb.OleDbConnection
protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}



回答5:


Yes. If it didn't, then it can't fully dispose of it's resources. BinaryReader, BinaryWriter, etc all close the underlying stream as well



来源:https://stackoverflow.com/questions/12034208/does-oledbconnection-dispose-close-the-connection

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