How to implement Commit and Rollback in LINQ?


To implement transaction(commit and Rollback) concept in LINQ

DbTransaction class and method provide the base of transaction. While inserting record using LINQ if error occur then rollback the record else commit the record.


Take an example UserMst table insertion



DbTransaction tranUserMst=null;
ExampleDataContext() objeDC = new ExampleDataContext();

try
{
objeDC.Connection.Open();
tranUserMst= objeDC.Connection.BeginTransaction();
objeDC.Transaction = tranUserMst;
UserMst objNewUserMst = new UserMst();
objNewUserMst.UserMstID = "MC01";
objNewUserMst.Name = "Muhil";
objeDC.UserMsts.InsertOnSubmit(objNewUserMst);
objeDC.Submitchanges();
tranUserMst.Commit();
}
catch(Exception ex)
{
tranUserMst.Rollback();
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: