I’ve read about TransactionScope and this article, but I still don’t understand 2 things:
- When
SqlCommand.ExecuteNonQueryis executed it doesn’t really executed untilscope.Complete()is invoked? If it’s true, so where all the operations that were executed within the scope are remain and wait forscope.Complete()orscope.Rollback()? - When
TransactionScopeis instantiated how it prevents fromSqlCommand.ExecuteNonQueryto be executed and wait forscope.Complete()orscope.Rollback()? Does it creates some “place” andSqlCommandsomehow knows about it and puts the instructions in there?
No this is not correct. Your command is executed on the line where you call
ExecuteNonQuery. It is, however, interesting to know where all the changes are stored. The changes do not go directly to the affected table(s) on the server side, rather the changes are stored in a temporary place (again on a server side), which leads to an answer on your second questionIt does not prevent as such, the action is executed, but because the result of the action is stored in a temporary location you must either merge these changes with the main table(s) –
scope.Commit()or discard these changes –scope.Rollback()(or whatever is used to discard the changes in the specific database data provider)