[dba-VB] c# so FINALLY ?

jwcolby jwcolby at colbyconsulting.com
Fri Jul 23 15:07:55 CDT 2010


The version that seems to addre4ss all of the issues:

namespace projBaseObjects
{
     class clsSQLReadWrite
     {
         public static void ExecuteNonQuery(string strCnn, string strSQL)
         {
             SqlConnection mCnn = null;
             SqlCommand myCommand = null;
             try
             {
                 mCnn = new SqlConnection(strCnn);
                 mCnn.Open();
                 myCommand = new SqlCommand(strSQL, mCnn);
                 myCommand.ExecuteNonQuery();
             }
             catch (SqlException) { throw; }
             catch (Exception) { throw; }
             finally
             {
                 if (myCommand != null)
                 {
                     myCommand.Dispose();
                     myCommand = null;
                 }
                 if (mCnn != null)
                 {
                     mCnn.Close();
                 }
             }
         }
     }
}

What do you do with the command object?  Does it need cleanup handling at all?  It doesn't have a 
close method, though it does have a dispose method.

And finally (pun intended) what should I do with this method?  Return a boolean true = worked?  But 
WHERE?  The finally will execute whether the function "worked" or not.  AFTER the finally block?  As 
I understand it that code would ONLY execute if the catch statements do not throw an error?  A throw 
does exit the function never to return correct?

Should I log (write to a log file) BEFORE the throw?  This is "closest to the error" and I can pass 
additional info about the context.  Or should I just continuously throw it all the way up to the top 
and log everything at the very top?

(mutters under breath - "I will learn this stuff")

-- 
John W. Colby
www.ColbyConsulting.com



More information about the dba-VB mailing list