Is there any case in which the following structure is needed?
using (Something something = new Something())
{
try
{
}
finally
{
something.SomeCleanup();
}
}
Or, should all cleanup tasks be performed in the implicit something.Dispose()
call?
Here is the offending code:
public static DataTable GetDataTable(string cmdText, IEnumerable<Parameter> parameters)
{
// Create an empty memory table.
DataTable dataTable = new DataTable();
// Open a connection to the database.
using (SqlConnection connection = new SqlConnection(ConfigurationTool.ConnectionString))
{
connection.Open();
// Specify the stored procedure call and its parameters.
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
command.CommandType = CommandType.StoredProcedure;
SqlParameterCollection parameterCollection = command.Parameters;
foreach (Parameter parameter in parameters)
parameterCollection.Add(parameter.SqlParameter);
try
{
// Execute the stored procedure and retrieve the results in the table.
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
try
{
dataAdapter.Fill(dataTable);
}
catch
{
dataTable.Dispose();
dataTable = null;
}
}
finally
{
//parameterCollection.Clear();
}
}
}
return dataTable;
}
NOTE: I have defined the Parameter
class so users of this function don't have to deal with the creation of SqlParameter
s directly. The SqlParameter
property of the Parameter
class can be used to retrieve an SqlParameter
.
At some point, my program does the following (cannot post the code, because it involves a lot of classes; basically, I have a mini-framework creating lots of objects):
- Create an array of
Parameter
s. GetDataTable('sp_one', parameters)
.GetDataTable('sp_two', parameters)
.