Will dbCommand.Close() close the connection as well?

Posted by J.W. on Stack Overflow See other posts from Stack Overflow or by J.W.
Published on 2009-09-14T19:19:14Z Indexed on 2010/06/17 18:33 UTC
Read the original article Hit count: 535

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about ADO.NET