PC Review


Reply
Thread Tools Rate Thread

SqlConnection, SqlTransaction with using, dispose and close

 
 
Steven Livingstone
Guest
Posts: n/a
 
      9th Aug 2003
After looking at a number of posting (many with close v dispose etc), I am
interested in whether the following code is the best way to ensure your
transaction behaves as expected and your connection gets closed as well as
connection and transaction both disposed.

using (SqlConnection conn = new SqlConnection(connectionstring))
{
using (SqlTransaction trans = conn.BeginTransaction())
{

try
{
Update1();
Update2();

trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
}
finally
{
conn.Close();
}
}
}


 
Reply With Quote
 
 
 
 
Steven Livingstone
Guest
Posts: n/a
 
      9th Aug 2003
From a post below, i guess this should be improved to :

using (SqlConnection conn = new SqlConnection(connectionstring))
{
using (SqlTransaction trans = conn.BeginTransaction())
{

try
{
Update1();
Update2();

trans.Commit();
}
catch (Exception ex)
{
try {
trans.Rollback();
}
catch (Exception e) {
//,,,
}
}
finally
{
conn.Close();
}
}
}


 
Reply With Quote
 
 
 
 
William Ryan
Guest
Posts: n/a
 
      9th Aug 2003
The method you definitely gets the job done. The only
thing I'd mention is that you will only rollback the
transaction based on the code in the two posts. You may
want to rethrow the exception so that the caller's handler
takes care of it, b/c if it fails, you probably want some
notification that it failed so you can respond
accordingly...but that's more of any exception handling
issue..as far as the transaction/ADO.NET side goes, looks
good.

Cheers,

Bill

W.G. Ryan
(E-Mail Removed)
www.knowdotnet.com

>-----Original Message-----
>After looking at a number of posting (many with close v

dispose etc), I am
>interested in whether the following code is the best way

to ensure your
>transaction behaves as expected and your connection gets

closed as well as
>connection and transaction both disposed.
>
>using (SqlConnection conn = new SqlConnection

(connectionstring))
>{
> using (SqlTransaction trans = conn.BeginTransaction())
> {
>
> try
> {
> Update1();
> Update2();
>
> trans.Commit();
> }
> catch (Exception ex)
> {
> trans.Rollback();
> }
> finally
> {
> conn.Close();
> }
> }
>}
>
>
>.
>

 
Reply With Quote
 
Steven Livingstone
Guest
Posts: n/a
 
      9th Aug 2003
Thanks Bill - and good luck with the knowdotnet site!

Steven
http://www.stevenlivingstone.com
http://www.venturetogether.org

"William Ryan" <(E-Mail Removed)> wrote in message
news:064e01c35e84$41ff99f0$(E-Mail Removed)...
> The method you definitely gets the job done. The only
> thing I'd mention is that you will only rollback the
> transaction based on the code in the two posts. You may
> want to rethrow the exception so that the caller's handler
> takes care of it, b/c if it fails, you probably want some
> notification that it failed so you can respond
> accordingly...but that's more of any exception handling
> issue..as far as the transaction/ADO.NET side goes, looks
> good.
>
> Cheers,
>
> Bill
>
> W.G. Ryan
> (E-Mail Removed)
> www.knowdotnet.com
>
> >-----Original Message-----
> >After looking at a number of posting (many with close v

> dispose etc), I am
> >interested in whether the following code is the best way

> to ensure your
> >transaction behaves as expected and your connection gets

> closed as well as
> >connection and transaction both disposed.
> >
> >using (SqlConnection conn = new SqlConnection

> (connectionstring))
> >{
> > using (SqlTransaction trans = conn.BeginTransaction())
> > {
> >
> > try
> > {
> > Update1();
> > Update2();
> >
> > trans.Commit();
> > }
> > catch (Exception ex)
> > {
> > trans.Rollback();
> > }
> > finally
> > {
> > conn.Close();
> > }
> > }
> >}
> >
> >
> >.
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the difference between SqlConnection.IDisposable.Dispose() and SqlConnection.Dispose() jinfeng_Wang@msn.com Microsoft Dot NET 1 18th Jan 2006 09:44 AM
If my System.IO.StreamWriter Write method throws "The specified network name is no longer available." and I try to Dispose or Close it in the finaly clause the close or dispose method just throws "The specified network name is no longe Daniel Microsoft Dot NET 3 8th Sep 2005 07:54 AM
If my System.IO.StreamWriter Write method throws "The specified network name is no longer available." and I try to Dispose or Close it in the finaly clause the close or dispose method just throws "The specified network name is no longe Daniel Microsoft Dot NET Framework 1 8th Sep 2005 04:11 AM
use of MySQLConnection.dispose and/or MySQLCommand.dispose Antonio Concepcion Microsoft ASP .NET 3 17th Feb 2005 07:33 PM
SqlTransaction and Dispose Wells Caughey Microsoft ADO .NET 2 4th Jun 2004 06:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:34 PM.