How to save on transaction?

Consider this scenario; Suppose I have a WPF window that has four objects associated with its controls (Schedule, Customer, Contract, ScheduleDetails and Signer) that represent four database tables in the database. I want that at the request of the user to save the information that he entered in order to join the operation with the atom, in another case, any save operation should be in one transaction, so that either all operations were successful or all failed. My question is the most efficient way to represent a transaction in this scenario.

+3
source share
1 answer

the most effective should use BeginTransaction, etc. on yours DbConnection, but that isn’t since you have to use the same connection and everyone DbCommandneeds to configure transaction , etc.

TransactionScope, and if you are on SQL Server 2005 or higher, you rarely notice a significant performance difference between this and BeginTransaction:

using(var tran = new TransactionScope()) {
    SaveA();
    SaveB();
    SaveC();
    SaveD();
    tran.Complete();
}

It doesn’t matter if it uses SaveAetc. one connection since it SqlConnectionwill automatically close at TransactionScope.

Alternatively, let ORM handle this for you; most of them will create transactions to save a group of changes.

, ; TransactionScope (DTC), ( WPF, ).

+4

Source: https://habr.com/ru/post/1726975/


All Articles