问题
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