简体   繁体   中英

How can i enable Transaction my codes with linqto SQL?

if writing below codes: Error returns.i do like advise :
http://stackoverflow.com/questions/794364/how-do-i-use-transactionscope-in-c
But only error change:

i am using windows server 2003.


 using (var stockMovementCtx = new StockMovementCtxDataContext())
            {
                using (var scope = new TransactionScope())
                {

// do something....
}
scope.Complete();
}

but if i changed my codes ; every thing is ok:


using (var stockMovementCtx = new StockMovementCtxDataContext())
            {


// do something....

}

How can i solve below error. This is really important.Please help me:((((

TransactionScope will elevate to DTC if necessary. It sounds like DTC is not correctly configured, usually due to firewall restrictions. Try a dtcping between the servers (important: in both directions).

DataContext by default wraps all operations within a Transaction , so you don't need to explicitly do Transaction while working with DataContext. Read this .

http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx

using (var stockMovementCtx = new StockMovementCtxDataContext())
{
     // do something....


     //everything until this point is within the transaction.
     stockMovementCtx.SubmitChange();
}

Why we need TransactionScope ?

TransactionScope enables you to perform transactions beyond dataabse. Say you have series of operations as below and all are atomic and they need be performed within a transaction.

1.) Add record in Table 1
2.) Add record in Table 2
3.) Write a NEW file on the Disk
4.) Rename file X on the disk
5.) Update file record in Table 3

If you use SqlTransaction then only the operaration 1,3 and 5 can participate in the transaction but 3 and 4 cannot because they do not relate database at all. In such cases TrasanctionScope can help you. TransactionScope leverages the MSDTC (Distributed Trasanction co-coordinator) that can help you do transactions beyond database context. You can wrap all five operations within a TransactionScope transaction execute them atomically. It is also worth noting that TransactionScope supports nested transactions, that means one block of transaction can contain multiple set of transactions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM