[dba-VB] c# so FINALLY ?

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Sat Jul 24 00:16:12 CDT 2010


Hi John --

Dispose does all the clean-up for SqlCommand object instance...

It doesn't make sense to have any SUCCESS/FAILURE return values from this
method as it has 

throw

calls inside catch blocks...

Such methods do usually return "RecordAffected" value if that needed by the
callers, some other execution stats, or nothing...

<<<
Or should I just continuously throw 
it all the way up to the top 
and log everything at the very top?
>>>
Again follow KISS-principle: 
- log exceptions as close as possible to the point where they appeared;
- return to the highest position in the call stack where you can gracefully
continue your application execution, or quit your app with a user-friendly
error message...

Logging all the way up to the top of the call stack would be similar to 

On Error GoTo ...

VB6/VBA approach - that one proved itself to be a professional approach for
VB6/VBA but for C#/VB.NET it's usually considered as a timid, pavid,
apprehensive and too expensive (in many areas) to be true approach, e.g. do
you take into account that try/catch/finally do influence your code
execution speed so putting them everywhere could degrade your code
performance?...

Code instrumentation/logging/profiling is usually done while going down to
the call stack, and .NET has special instrumentation classes, which can be
used to organize as detailed as needed logging depending on current
application settings...

After all - your application usually work or crash when deployed? - if the
latter then whatever advanced logging/code instrumentation approach you'll
use you'll anyway loose your customers (pun not intended)...

Be courageous! Forget "VB6/VBA timidity", recall your best times when you've
been a "code cowboy" but do use Unit Testing (at least) - then you'll make
your coding style light, your code running lightning fast and still being
"bullet-proof"...

Thank you. :)

-- Shamil

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Saturday, July 24, 2010 12:08 AM
To: VBA
Subject: [dba-VB] c# so FINALLY ?

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
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list