Does DbConnection.Dispose() call Close()?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi;

If I do using (DbConnection conn = ...) { ... code ... }

That will call dispose when it exits the using. Does that dispose call Close
(if the connection is open) and will it do so even on an exception? Reading
the docs I assume it does, but I can't find anywhere that it says absolutely
it does.
 
David said:
Hi;

If I do using (DbConnection conn = ...) { ... code ... }

That will call dispose when it exits the using. Does that dispose call Close
(if the connection is open) and will it do so even on an exception? Reading
the docs I assume it does, but I can't find anywhere that it says absolutely
it does.

Here is how IDbConnection.Dispose() is implemented (as Reflector utility shows):

SqlClient:
protected override void Dispose(bool disposing)
{
if (disposing)
{
switch (this._objectState)
{
case ConnectionState.Open:
{
this.Close();
break;
}
}
this._constr = null;
}
base.Dispose(disposing);
}

Odbc:
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._constr = null;
this.Close();
CNativeBuffer buffer1 = this._buffer;
if (buffer1 != null)
{
buffer1.Dispose();
this._buffer = null;
}
}
base.Dispose(disposing);
}

OleDb:
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this.objectState != 0)
{
this.DisposeManaged();
if (base.DesignMode)
{
OleDbConnection.ReleaseObjectPool();
}
this.OnStateChange(ConnectionState.Open, ConnectionState.Closed);
}
if (this.propertyIDSet != null)
{
this.propertyIDSet.Dispose();
this.propertyIDSet = null;
}
this._constr = null;
}
base.Dispose(disposing);
}
 
David,

That will call dispose when it exits the using. Does that dispose call
Close
(if the connection is open) and will it do so even on an exception?
Reading
the docs I assume it does, but I can't find anywhere that it says
absolutely
it does.
Yes and as extra does it remove the by you added connection.string as showed
in the documentation.

Cor
 
Back
Top