M
MLynch
I have a question regarding whether to use "Using" when connecting to SQL
Server via ASP.NET
I noticed in Microsoft's PetShop reference application they used the "Using"
keyword to ensure proper disposal of the connection object. But by
employing the "Using" keyword do you negate the benefits of the DB
Connection Pool. Or are connection pools even used in ASP.NET due the
statelessness of the web requests?
So to use using or not in asp.net?
- M Lynch
--- USE THIS ---
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("spFunction", conn);
command.CommandType = CommandType.StoredProcedure;
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
--- OR JUST THIS ---
try
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("spFunction", conn);
command.CommandType = CommandType.StoredProcedure;
conn.Open();
command.ExecuteNonQuery();
}
finally
{
conn.Close();
}
Server via ASP.NET
I noticed in Microsoft's PetShop reference application they used the "Using"
keyword to ensure proper disposal of the connection object. But by
employing the "Using" keyword do you negate the benefits of the DB
Connection Pool. Or are connection pools even used in ASP.NET due the
statelessness of the web requests?
So to use using or not in asp.net?
- M Lynch
--- USE THIS ---
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("spFunction", conn);
command.CommandType = CommandType.StoredProcedure;
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
--- OR JUST THIS ---
try
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("spFunction", conn);
command.CommandType = CommandType.StoredProcedure;
conn.Open();
command.ExecuteNonQuery();
}
finally
{
conn.Close();
}