I've seen this in some legacy code which generates code analysis warnings:
Database db = DatabaseFactory.CreateDatabase(strDBCon);
DbCommand dbCommand = db.GetSqlStringCommand(sb.ToString());
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
while (dataReader.Read())
{
}
dataReader.Close(); // <-- this is redundant as close is covered by using's implicit dispose
}
dbCommand.Connection.Close();
dbCommand.Dispose();
db = null;
I've read here on SO that the dbCommand.Connection property must be closed in addition to disposing of the dbCommand. I would have thought that dbCommand would dispose of any child disposable objects, in this case Connection. If this is not the case, why?
strDBConis a string, so that means the Database object is creating the connection object, which to me implies that it isDisposableand when disposed it would dispose the connection that it created.