I have the following code, If I use conn == null in finally do I still use connection pooling? I know it is a good practice to close your connection but how about disposing the whole connection object?
public void ExecuteNonQuery(SqlCommand Cmd)
{
//========== Connection ==========//
SqlConnection Conn = new SqlConnection(strConStr);
try
{
//========== Open Connection ==========//
Conn.Open();
//========== Execute Command ==========//
Cmd.Connection = Conn;
Cmd.CommandTimeout = 180;
Cmd.ExecuteNonQuery();
}
catch (Exception Exc)
{
throw Exc;
}
finally
{
//======== Closing Connection ========//
if (Conn.State == ConnectionState.Open)
{ Conn.Close(); }
//======== Disposing object ========//
Conn = null;
}
}
First, consider using using
, second, let the framework handle disposal. The managed providers will do pooling based on the connection string.
public void ExecuteNonQuery(SqlCommand Cmd)
{
//========== Connection ==========//
using(SqlConnection Conn = new SqlConnection(strConStr))
{
//========== Open Connection ==========//
Conn.Open();
//========== Execute Command ==========//
Cmd.Connection = Conn;
Cmd.CommandTimeout = 180;
Cmd.ExecuteNonQuery();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With