Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to Close() a SQLConnection before it gets disposed?

People also ask

Do you need to close SqlConnection?

An application can call Close more than one time. No exception is generated. If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose .

What happens if SqlConnection is not closed?

Answers. Not closing connections could cause timeouts as the connection pool may run out of available connections that can be used.

Does disposing a SQL connection close it?

When you call "Dispose()" on a SqlConnection, internally, it calls "Close()", too. There is no worry - you are free to use Close() manually, or just let Dispose() do it for you.

Does using automatically close SqlConnection?

Answers. Yes. When the using block ends, the connection automatically closes (That is what IDisposable is for). So, do not close the connection explicitly.


Since you have a 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);
}

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


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);
}

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);
}

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

MSDN - SqlConnection.Dispose()


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


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)