views:

778

answers:

8

Per my other question here about Disposable objects, should we call Close() before the end of a using block?

using (SqlConnection connection = new SqlConnection())
using (SqlCommand command = new SqlCommand())
{
    command.CommandText = "INSERT INTO YourMom (Amount) VALUES (1)";
    command.CommandType = System.Data.CommandType.Text;

    connection.Open();
    command.ExecuteNonQuery();

    // Is this call necessary?
    connection.Close();
}
+1  A: 

No, calling Dispose() on SqlConnection also calls Close().

MSDN - SqlConnection.Dispose()

Justin Niessner
A: 

No, the SqlConnection class inherits from IDisposable, and when the end of using (for the connection object) is encountered, it automatically calls the Dispose on the SqlConnection class.

FYI, this wasn't really helpful because you didn't explain WHY the call to Close is unnecessary. Thanks for the input though.
SkippyFire
+2  A: 

Hi there.

No, having the Using block calls Dispose() for you anyway, so there is no need to call Close().

Jas.

Jason Evans
FYI, this wasn't really helpful because you didn't explain WHY the call to Close is unnecessary. Thanks for the input though.
SkippyFire
Sorry, I should have said that for most objects that implement IDisposable and have a Close() method, calling Close() ends up calling Dispose() behind the scenes for you anyway.
Jason Evans
+2  A: 

No, it is not necessary to Close a connection before calling Dispose.

Some objects, (like SQLConnections) can be re-used afer calling Close, but not after calling Dispose. For other objects calling Close is the same as calling Dispose. (ManualResetEvent and Streams I think behave like this)

pipTheGeek
+12  A: 

Since you have an using block, the Dispose method of the SQLCommand will be called and it will close the connection:

// System.Data.SqlClient.SqlConnection.Dispose disassemble
protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}
CMS
+4  A: 

Disassembly of SqlConnection from using .NET Reflector:

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

}

It calls Close() inside of Dispose()

statenjason
@statenjason: could you please say that how do you take advantage of using disassemblers line .net reflector?
odiseh
@odiseh just download .NET Reflector, run reflector.exe, and you can open any .net DLL (including the standard library). It provides you with a tree structure similar to Visual Studio's object browser, however, you can right click on any class or method and click "disassemble" it will then return the source to you in C# or VB, whichever you've selected in the options.
statenjason
@statenjason: Thank you.
odiseh
+1  A: 

Using Reflector, you can see that the Dispose method of SqlConnection actually does call Close();

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}
Aaron Daniels
+1  A: 

The using keyword will close the connection correctly so the extra call to Close is not required.

From the MSDN article on SQL Server Connection Pooling:

"We strongly recommend that you always close the connection when you are finished using it so that the connection will be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C#"

The actual implementation of SqlConnection.Dispose using .NET Reflector is as follows:

// System.Data.SqlClient.SqlConnection.Dispose disassemble
protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}
Thomas Bratt