Jonas Nyrup Jonas Nyrup - 11 days ago 4
C# Question

Should SQLConnection from ReliableSqlConnection be closed?

Every example I have found on using

ReliableSqlConnection
looks similar to:

using (var cnn = new ReliableSqlConnection(connString))
{
using (var cmd = cnn.CreateCommand())
{
cnn.Open();
...
}
}


As
Open()
returns a
SQLConnection
which is
IDisposable
, shouldn't it be disposed?

Would it make any difference or harm, if I encapsulate in a using statement?

using (var cnn = new ReliableSqlConnection(connString))
{
using (var cmd = cnn.CreateCommand())
{
using(cnn.Open(){
...
}
}
}

Answer

You do not need to dispose, the deprecated ReliableSQLConnection class creates the SqlConnection in its ctor and disposes of it in its Dispose():

private void Dispose(bool disposing)
{
    if (disposing)
    {
        if (this.underlyingConnection.State == ConnectionState.Open)
        {
            this.underlyingConnection.Close();
        }

        this.underlyingConnection.Dispose();
    }
}
Comments