Will dbCommand.Close() close the connection as well?
- by J.W.
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;
}
}