• A2ZMenu.Com
Loading
  • Home
  • Blogs
    • AJAX
    • ASP.NET
    • BI
    • CSharp
    • JavaScript
    • LINQ
    • Miscellaneous
    • SQL
    • SharePoint
    • Silverlight
    • WCF
  • Tutorials
    • HTML5
    • ASP.NET 4.0
    • CSharp
    • SharePoint
    • SQL Function
  • Online Exam
    • ASP.NET
    • LINQ
    • Silverlight
    • SQL
    • JavaScript
    • SharePoint
  • Forum
  • Utility
  • Fun @ Work
    • Appraisal
    • Funny Images
    • Funny Puzzle
    • Miscellaneous
    • Motivating Stories
  • Wiki
  • Contact Us

Test Your Skill

Show/Hide
 Q. Out of Boxing and Unboxing which one is implicit?
View all questions...

Maintain Transaction in LINQ to SQL

Hello Folks,

Today I will tell you how to maintain transaction in LINQ to SQL.

Before going on to discuss the detail of maintaining Transaction, it should be made clear that
 LINQ to SQL implicitly maintain transaction

So if there is single call to SubmitChanges() after any number of calls to InsertOnSubmit, DeleteOnSubmit...  

But if you have multiple call to SubmitChanges(), LINQ will not maintain Transaction and one has to explicitly
maintain Transaction. And that is what we are going to discuss today.


In the example I am going to use, I have taken two Table2 and TestTable.

First Create object of Type System.Data.Common.DbTransaction

System.Data.Common.DbTransaction transaction;

Now Open the connection. Make sure to use the same DataContext to open connection which you are going to use
In your LINQ operation.

DataClasses1DataContext dataContext = new DataClasses1DataContext();
dataContext.Connection.Open();

Now begin the transaction and assign it to transaction object created in first step.

transaction = dataContext.Connection.BeginTransaction();

Go on and set the dataContext Transaction property to transaction.

dataContext.Transaction = transaction;

At this point 50% of job is done.

In the try block add the logic to insert the data into the and just after SubmitChanges() add

transaction.Commit();

If transaction.Commit() line is missing database operation performed will not be reflected in database

In the Catch block add the code

transaction.Rollback();

and final step

Don’t forget to close the connection you  opened at the top

Find below the complete code

static void Main(string[] args)
        {
            DataClasses1DataContext dataContext = new DataClasses1DataContext();
            System.Data.Common.DbTransaction transaction;
            dataContext.Connection.Open();
            transaction = dataContext.Connection.BeginTransaction();
            dataContext.Transaction = transaction;

            try
            {
                Table2 tbl2 = new Table2()
                {
                    ID = 1,
                    Value = "Test Data"
                };

                dataContext.Table2s.InsertOnSubmit(tbl2);

                TestTable tstTbl = new TestTable()
                {
                    ID = 1,
                    Name = "Alpha",
                    lastname = "Beta"
                };

                dataContext.TestTables.InsertOnSubmit(tstTbl);

                dataContext.SubmitChanges();

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                Console.WriteLine("Rolback Done");
            }
            finally
            {
                if (null != dataContext.Connection)
                {
                    dataContext.Connection.Close();
                }
            }
        }

This is all that needs to be done to maintain Transaction in LINQ.

Happy Coding J



Add Comment

Comment
Name
 (Required)
E-mail (Privacy assured)
 (Required)

Poll of the Day

  Who is best CEO so far?
 
 
 

Related Contents

Online Test Papers

  • ASP.NET
  • JavaScript
  • LINQ
  • SharePoint
  • Silverlight
  • SQL

Related Tutorials

  • HTML 5 Tutorial
  • SharePoint Best Practice
  • ASP.NET 4.0 Tutorials

Connect with Facebook

Top Blogs

  • SharePoint 2010 Model Dialog
  • Validation of viewstate MAC failed. If this application is hosted...
  • Silverlight: ListBox with CheckBox
  • Creating a Windows Service in C#
  • Using InstallUtil to install/uninstall service
  • How to call webservice using Ajax or JavaScript
  • LINQ to SQL Best Practice
  • The contract name could not be found in the list of contracts implemented by the service
  • Calling JavaScript function from C#
Blogs
  • AJAX
  • ASP.NET
  • BI
  • CSharp
  • HTML
  • JavaScript
  • LINQ
  • SQL Server
  • SharePoint
  • Silverlight
Tutorials
  • HTML5
  • ASP.NET 4.0
  • CSharp
  • SharePoint
  • SQL Server
Online Examinations
  • ASP.NET
  • LINQ
  • SQL Server
  • JavaScript
  • SharePoint
Online Utilities
  • Age Calculator
  • Happy Birthday Wisher
  • Independence Day Wisher
  • URL Encoder Decoder
  •  
Fun @ Work
  • Appraisal
  • Funny Images
  • Funny Puzzle
  • Miscellaneous
  • Motivating Stories
A2ZMenu © 2012 Home | Contact Us | Privacy Policy | Connect with Facebook | Follow us on Twitter