I'm currently using this block of code that gets called in the "Finally" blocks of my data handler class for connecting to SQL Server (2008):
What I'm wondering is whether the order I'm cleaning up objects matters. Currently, it's SqlConnection, SqlCommand, and SqlAdapter. Is there a better or preferred order for cleaning up private database connection objects?
Code:
private void CleanupPrivateComplexObjects()
{
if (this._sqlConnection != null)
{
if (this._sqlConnection.State != ConnectionState.Closed)
{
this._sqlConnection.Close();
}
this._sqlConnection.Dispose();
this._sqlConnection = null;
}
if (this._sqlCommand != null)
{
this._sqlCommand.Dispose();
this._sqlCommand = null;
}
if (this._sqlAdapter != null)
{
this._sqlAdapter.Dispose();
this._sqlAdapter = null;
}
}
What I'm wondering is whether the order I'm cleaning up objects matters. Currently, it's SqlConnection, SqlCommand, and SqlAdapter. Is there a better or preferred order for cleaning up private database connection objects?