Operator
A using should be used every time you create an instance of a class that implements IDisposable within the scope of the block. This ensures that the Dispose() method will be called on this instance, regardless of whether an exception is thrown.
In particular, your code only catches managed exceptions and then destroys the stack frame, causing a new exception instead of re-creating the existing one.
The correct way to do this is:
using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["T3"])) { cn.Open(); using (SqlTransaction tr = cn.BeginTransaction()) {
Note that if your class has instances of instances of types that implement IDisposable , then your class must implement IDisposable itself and delete these members during its own call to Dispose() .
John Saunders Jul 14 '09 at 20:30 2009-07-14 20:30
source share