.NET Transactionscope Options

I am new to C #. So I'm just wondering if anyone can help me figure out how C # works with a transaction? Because I'm a little confused by the definition. However, let me tell you a little about my problem. So you know what I'm trying to achieve.

I have three table adapters declared for three different datasets, such as:

logTableAdapter logAdap = new logTableAdapter(); measTableAdapter measAdap = new measTableAdapter(); valueTableAdapter valueAdap = new valueTableAdapter(); 

Data Import Process:

  • First, I insert a log entry using the logAdap.insert () method.
  • Scroll through the excel file to capture the dimensions and start the insert using the measAdap.insert () method.
  • Preliminary measurement. I insert values ​​through the valueAdap.insert () method.

So my question is, since dimension and value have nested relationships. How can I create a nested transaction and when an error occurs somewhere (insert I / O), I just want to undo everything I did. That is, I just want to go back to the point before I inserted the journal entry.

+8
c # transactionscope
source share
3 answers

By specifying this aptly named article: The final column is TableAdapters + Transactions .

if you work with several operations within one TransactionScope, that is, GetData and Update both inside one TransactionScope and two updates in TransactionScope, you will effectively open two SqlConnections in the same database and, therefore, unjustifiably push the transaction from LTM to MSDTC As a best practice, ALWAYS wrap only a transaction exclusively in TransactionScope. If you decide to wrap several operations inside the same TransactionScope, you must in this case independently manage the connection lifetime by expanding the definition of a partial class. In other words, the following code will force the transaction to advance -

 using (TransactionScope tsc = new TransactionScope()) { tableAdap.GetData() ; //Do your transactional work. tableAdap.Update() ; tsc.Complete() ; } 

But the following code is just good -

 using (TransactionScope tsc = new TransactionScope()) { tableAdap.OpenConnection() ; tableAdap.GetData() ; //Do your transactional work. tableAdap.Update() ; tableAdap.CloseConnection() ; tsc.Complete() ; } 

So you only need one TransactionScope , but with some caveats. Here is the gist, but I recommend you read the blog post.

TableAdapters are not the most appropriate data access methodology for high-performance transaction systems. If you need more reliability, you should probably write your operation as a stored procedure and execute it from your C # code.

+2
source share

you don’t need more than one TransactionScope, you can do everything in the same thing that I think.

0
source share

As I think you were looking for how to use TransactonScope, here is what your code looks like, changing the example in your comment a bit:

 using( TransactionScope ts = new TransactionScope() ) { try { logAdap.InsertLog(.....); foreach (.....) { measAdap.InsertMeas(.....); foreach (.....) { valAdap.InsertVal(.....); } } // Complete the transaction ts.Complete(); } catch (Exception ex) { // Your error handling here. // No need to rollback each table adapter. That along with all the // transaction is done for you when exiting the using block without // calling Complete() on the TransactionScope. }} 

This method of using a scope is called implicit transactions, and you can get a good overview of this in the MSDN article: Implementing an Implicit Transaction Using a Transaction Scope .

Having said that, what fencliff means is true, since you probably don't want to open multiple connections for your script, saving valuable database resources.

0
source share

All Articles