Saturday, September 3, 2011

Deletes and inserts when using TransactionScope

he TransactionScope has three modes:

1.Required:If a transaction already exists, then the scope of this transaction will join the existing transaction. Otherwise, it will create its own affairs.

2.RequiresNew:The scope of this transaction will create their own affairs.

3.Suppress:If it is within the scope of the current active transaction, the transaction will neither join the ambient transaction nor create their own affairs. As part of the code need to stay outside in the transaction, you can use this option.



So you can change your code as below 

 
// --------------------------
// Insert Method
// --------------------------
protected void Insert()
{
    using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        // Code for inserting records.
        scope1.Complete();
    }
}
// --------------------------
// Delete Method
// --------------------------
protected void Delete()
{
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        // Code for deleting records.
        scope2.Complete();
    }
}
// --------------------------
// Update Method
// --------------------------
protected void Update()
{
using (TransactionScope scope3 = new TransactionScope(TransactionScopeOption.Required))
    {
        Delete();
        Insert();
        scope3.Complete();
    }
}
ref:http://forums.asp.net/p/1590144/4183122.aspx

No comments:

Post a Comment

Devops links

  Build Versioning in Azure DevOps Pipelines