Tight disposing of objects in an old project
-
using (SqlConnection Connection = Sql.GetConnectionString())
{
using (SqlCommand command = new SqlCommand("*****", Connection))
{
command.CommandType = CommandType.StoredProcedure;
try
{
Connection.Open();
intID = (int) command.ExecuteScalar();
}
catch (Exception ex)
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
finally
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
}
}___ ___ ___
|__ |_| |\ | | |_| \ /
__| | | | \| |__| | | / -
using (SqlConnection Connection = Sql.GetConnectionString())
{
using (SqlCommand command = new SqlCommand("*****", Connection))
{
command.CommandType = CommandType.StoredProcedure;
try
{
Connection.Open();
intID = (int) command.ExecuteScalar();
}
catch (Exception ex)
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
finally
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
}
}___ ___ ___
|__ |_| |\ | | |_| \ /
__| | | | \| |__| | | /Well, at least they're being sure the stuff is disposed of. Too bad they also disposed of their ability to understand how Try/Catch works too.
A guide to posting questions on CodeProject
How to debug small programs
Dave Kreskowiak -
using (SqlConnection Connection = Sql.GetConnectionString())
{
using (SqlCommand command = new SqlCommand("*****", Connection))
{
command.CommandType = CommandType.StoredProcedure;
try
{
Connection.Open();
intID = (int) command.ExecuteScalar();
}
catch (Exception ex)
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
finally
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
}
}___ ___ ___
|__ |_| |\ | | |_| \ /
__| | | | \| |__| | | /Pretty clear developer intent. Yet it's also a scream for help.
You'll never get very far if all you do is follow instructions.
-
Well, at least they're being sure the stuff is disposed of. Too bad they also disposed of their ability to understand how Try/Catch works too.
A guide to posting questions on CodeProject
How to debug small programs
Dave KreskowiakDave Kreskowiak wrote:
disposed of their ability to understand how Try/Catch works
and
using
also... -
using (SqlConnection Connection = Sql.GetConnectionString())
{
using (SqlCommand command = new SqlCommand("*****", Connection))
{
command.CommandType = CommandType.StoredProcedure;
try
{
Connection.Open();
intID = (int) command.ExecuteScalar();
}
catch (Exception ex)
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
finally
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
}
}___ ___ ___
|__ |_| |\ | | |_| \ /
__| | | | \| |__| | | /Bloody beginners!
using (SqlConnection Connection = Sql.GetConnectionString()) { using (SqlCommand command = new SqlCommand("\*\*\*\*\*", Connection)) { command.CommandType = CommandType.StoredProcedure; try { Connection.Open(); intID = (int) command.ExecuteScalar(); // Do not forget to dispose! command.Dispose(); Connection.Close(); Connection.Dispose(); } catch (Exception ex) { //there is a catch: intID = 22; command.Dispose(); Connection.Close(); Connection.Dispose(); } finally { command.Dispose(); Connection.Close(); Connection.Dispose(); } } }
Also a great idea that
Sql.GetConnectionString()
does not return astring
but aSqlConnection
. -
using (SqlConnection Connection = Sql.GetConnectionString())
{
using (SqlCommand command = new SqlCommand("*****", Connection))
{
command.CommandType = CommandType.StoredProcedure;
try
{
Connection.Open();
intID = (int) command.ExecuteScalar();
}
catch (Exception ex)
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
finally
{
command.Dispose();
Connection.Close();
Connection.Dispose();
}
}
}___ ___ ___
|__ |_| |\ | | |_| \ /
__| | | | \| |__| | | /