Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using statement and Close methods

Tags:

c#

.net

Does the using statement really call the close method when used with a database connection object? The MSDN documentation says it ensures that the Dispose method is called but makes no mention of close. I see posts on Stack Overflow where people say that it does both. Does someone have a concrete answer one way or another on this from Microsoft or other solid proof that it does?

like image 600
James Avatar asked Apr 02 '09 03:04

James


1 Answers

Here is the "Dispose" method of the SqlConnection class:

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

As you can see, it does indeed call Close()

like image 69
DancesWithBamboo Avatar answered Oct 20 '22 03:10

DancesWithBamboo