8

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?

Community
  • 1
  • 1
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • Why do you think there is a difference? – Amiram Korach Aug 20 '12 at 08:12
  • Yes. Instead of calling .Dispose, try the `using` block which will dispose the connection, as the code inside the `using` block finishes execution. – shahkalpesh Aug 20 '12 at 08:12
  • @AmiramKorach: people have talked only about `SqlConnection`. `DbConnection` is an abstract class so it depends on the actual implementation of `OledbConnection`. – CJ7 Aug 20 '12 at 08:13

5 Answers5

5

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.

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
3

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().

Tadeas Kriz
  • 524
  • 4
  • 14
2

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.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
1

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);
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

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

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74