0

I have a project that isn't using USING anywhere with their ADO.NET code. I am cleaning up their unclosed connections. Is the below code a best practice with try/catch/finally. I also have some that contains SqlTransaction that I'm disposing in between the command and connection dispose.

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyNGConnectDashBoardConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand();
            DataSet ds = new DataSet();
            try
            {
                con.Open();
                cmd.Connection = con;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);

            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                cmd.Dispose();
                con.Dispose();
            }
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • You just asked a very similar question here: http://stackoverflow.com/questions/9525307/does-sqltransaction-need-to-have-dispose-called – Cᴏʀʏ Mar 01 '12 at 22:49

3 Answers3

1

Actually, there is no need to worry about closing the connection when using the SqlDataAdapter.Fill(dataset) method. This method closes the connection after performing every Fill.

Also, there is no need to call SqlCommand.Dispose() since the command itself has no unmanaged resources to clean up. What you should be concerned about is if SqlConnection.Close() is called at some point. This is done after Fill.

Nachiket Mehta
  • 310
  • 3
  • 15
0

The best practice is using using instead of try/finally :)

However in your case even using is not needed, because Fill() closes the connection:

        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyNGConnectDashBoardConnectionString"].ToString())
        SqlDataAdapter da = new SqlDataAdapter("your sql is here", con);
        da.Fill(ds);

Also simple re-throwing exceptions makes no sense at all. If you need to log error, just use plain throw; as @Cory suggested.

the_joric
  • 11,986
  • 6
  • 36
  • 57
0

What you have is fine. It is always a good idea to dispose of objects that use unmanaged resources. However, if you get sick of always explicitly calling Dispose, the best practice is probably to use the using:

using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyNGConnectDashBoardConnectionString"].ToString()))
{
    using (SqlCommand cmd = new SqlCommand())
    {
        DataSet ds = new DataSet();
        try
        {
            con.Open();
            cmd.Connection = con;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);

        }
        catch (Exception ex)
        {
            throw; // I changed this too!
        }
    }
}

Also, you almost always want to simply throw if you're going to "rethrow" an exception. You lose some of your stack trace if you throw ex;.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194