views:

779

answers:

4

I have the following ado.net code, if I already use using to wrap my DBCommand, do I have to close the underneath connection explicitly?

Thanks,

       public static void ExecuteSQL(int Id)
        {
            Database db;
            const string sqlCommand = "Stored Procedure Name";
            try
            {
                db = DatabaseFactory.CreateDatabase();
                using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
                {
                    db.AddInParameter(dbCommand, "p_Id", DbType.Int32, Id);
                    db.ExecuteNonQuery(dbCommand);
**//do I have to close connection explicitely here??**
                    if (dbCommand.Connection.State != ConnectionState.Closed)
                    {
                        dbCommand.Connection.Close();
                    }
                    dbCommand.Connection.Dispose();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex.StackTrace, ex);
                throw;
            }
        }
+1  A: 

No, you do not.

Once the end of the using block is hit, dispose is called for you.

cptScarlet
I have no idea why this is being upvoted, as it's just plain wrong. You absolutely have to close the connection. This question is about having to dispose the **connection**, not the **command**. "using" the command alone does not do so.
Adam Robinson
Based on the comment in his code and the fact that he is explicitly calling dbCommand.Connection.Close and dbCommand.Connection.Dispose() I though he was asking if that was needed since he was already wrapping the command in a using block.If he was asking if he still needs to call something like db.Close and db.Dispose then you are 100% correct. I just did not think that is what his question was.
cptScarlet
"if I already use using to wrap my DBCommand, do I have to close the *underneath connection* explicitly" That seems pretty clearly worded.
Adam Robinson
And his comment in the code, again, refers to explicitly closing the connection (which he's doing).
Adam Robinson
And, again, calling dbCommand.Dispose() (which is what the using block does), does **not** call dbCommand.Connection.Dispose().
Adam Robinson
+4  A: 

Yes, if all you have wrapped in the using block is the DbCommand, then you will have to explicitly close the DbConnection, as you currently do in your code. It is sufficient, however, simply to call Dispose. You do not need to call both Close and Dispose.

Adam Robinson
A: 

I think this is an excellent question. And one that I would like to know more about. Can people please contribute more to this story?

I agree you need the using for the DbCommand and think that the underlying connection must be closed and disposed of. But the way its done above doesn't appear to be a robust solution to me. In the event an exception occurs the connection is not disposed or closed.

Obviously it would be ideal if we could wrap the DbConnection in a using statement as well but the architecture of the Enterprise Library Data Access Application Block appears to encapsulate the connection so we can't do that.

Note sure an 'answer' was the best way to post this comment as I'm fairly new to SO let me know if there is a better way to add content like this.

Thanks, Justin

Justin
Try 'add comment' next time
mgroves
A: 

http://www.willydev.net/descargas/WillyDev_EntLib_TestGuide.pdf

The application block closes the database connections after it is finished with them. For example, the implementation of the ExecuteNonQuery method includes a using statement. The using statement obtains resources, executes a statement, and disposes of the resources. In this case, the resource is the database connection. In the case of the ExecuteReader method, the application block uses the Command-Behavior.CloseConnection method to close the connection after the reader closes.

Satya