Click here to Skip to main content
15,889,595 members
Home / Discussions / C#
   

C#

 
QuestionAutomatic Server Shutdown Pin
FJJCENTU29-Mar-10 0:18
FJJCENTU29-Mar-10 0:18 
AnswerRe: Automatic Server Shutdown Pin
DaveyM6929-Mar-10 0:48
professionalDaveyM6929-Mar-10 0:48 
AnswerRe: Automatic Server Shutdown Pin
David Skelly29-Mar-10 1:56
David Skelly29-Mar-10 1:56 
AnswerRe: Automatic Server Shutdown Pin
Dave Kreskowiak29-Mar-10 3:43
mveDave Kreskowiak29-Mar-10 3:43 
QuestionRemoving items from list while performing operations on it Pin
xkrja29-Mar-10 0:17
xkrja29-Mar-10 0:17 
AnswerRe: Removing items from list while performing operations on it Pin
Keith Barrow29-Mar-10 0:47
professionalKeith Barrow29-Mar-10 0:47 
AnswerRe: Removing items from list while performing operations on it Pin
SeMartens29-Mar-10 0:48
SeMartens29-Mar-10 0:48 
Questionusing (SqlTransaction... ) Pin
Dewald29-Mar-10 0:12
Dewald29-Mar-10 0:12 
For much too long I have not used Transactions in my DB apps - let's not go into how stupid that is.

So I'm working on an existing app and trying to wrap Transactions around sequences of SqlCommands that I need to keep atomic. The approach I've followed thus far is something like:
SqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction()
try
{
   // First SQL command that is part of an atomic transaction
   using (SqlCommand sqlCommand = new SqlCommand("...", mySqlConnection, mySqlTransaction))
   {
      mySqlCommand.ExecuteScalar(); // or whatever
   }

   // Second SQL command that is part of an atomic transaction
   using (SqlCommand sqlCommand = new SqlCommand("...", mySqlConnection, mySqlTransaction))
   {
      mySqlCommand.ExecuteScalar(); // or whatever
   }

   mySqlTransaction.Commit();
}
catch
{
   mySqlTransaction.Rollback();
}


Then, as I reached the halfway mark (as always happens), it occurred to me that, surely, you can wrap the SqlTransaction in a using block as well. I can't seem to find thorough documentation on how to do that though. Could someone help out perhaps?

If you put your SqlTransaction in a using block, when is it committed? What happens if an exception occurs? Will it automatically roll back?
AnswerRe: using (SqlTransaction... ) Pin
Eddy Vluggen29-Mar-10 1:06
professionalEddy Vluggen29-Mar-10 1:06 
AnswerRe: using (SqlTransaction... ) Pin
Keith Barrow29-Mar-10 1:07
professionalKeith Barrow29-Mar-10 1:07 
AnswerRe: using (SqlTransaction... ) Pin
PSK_29-Mar-10 1:08
PSK_29-Mar-10 1:08 
GeneralRe: using (SqlTransaction... ) Pin
Dewald29-Mar-10 1:51
Dewald29-Mar-10 1:51 
GeneralRe: using (SqlTransaction... ) Pin
PSK_29-Mar-10 3:06
PSK_29-Mar-10 3:06 
GeneralRe: using (SqlTransaction... ) Pin
Dewald29-Mar-10 3:33
Dewald29-Mar-10 3:33 
AnswerRe: using (SqlTransaction... ) Pin
PIEBALDconsult29-Mar-10 4:14
mvePIEBALDconsult29-Mar-10 4:14 
GeneralRe: using (SqlTransaction... ) Pin
Dewald29-Mar-10 21:27
Dewald29-Mar-10 21:27 
GeneralRe: using (SqlTransaction... ) Pin
PIEBALDconsult30-Mar-10 4:27
mvePIEBALDconsult30-Mar-10 4:27 
QuestionHow does CLR differentiates between value and reference types Pin
Praveen Raghuvanshi28-Mar-10 23:00
professionalPraveen Raghuvanshi28-Mar-10 23:00 
AnswerRe: How does CLR differentiates between value and reference types Pin
PSK_28-Mar-10 23:52
PSK_28-Mar-10 23:52 
AnswerRe: How does CLR differentiates between value and reference types Pin
Keith Barrow29-Mar-10 0:06
professionalKeith Barrow29-Mar-10 0:06 
GeneralRe: How does CLR differentiates between value and reference types Pin
Praveen Raghuvanshi29-Mar-10 1:39
professionalPraveen Raghuvanshi29-Mar-10 1:39 
AnswerRe: How does CLR differentiates between value and reference types Pin
Gideon Engelberth29-Mar-10 2:55
Gideon Engelberth29-Mar-10 2:55 
AnswerRe: How does CLR differentiates between value and reference types Pin
PIEBALDconsult29-Mar-10 4:17
mvePIEBALDconsult29-Mar-10 4:17 
AnswerRe: How does CLR differentiates between value and reference types Pin
harold aptroot29-Mar-10 5:07
harold aptroot29-Mar-10 5:07 
AnswerRe: How does CLR differentiates between value and reference types Pin
Tej Aj29-Mar-10 5:25
Tej Aj29-Mar-10 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.