Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I an new in linq i want to use commit rollback tsql in linq using c# 4.0
Posted

before any changes occure on the database you have to have a db.submitchanges() before the changes are submitted. Anything that happens in the mean-time will be held in a transaction anyway. :

C#
public void ChangeData(string newData)
{

            //Always use a new instance of the DataContext when making changes to the data as all changes made would be submitted otherwise
            DataContext1 db = new DataContext1();

            //throws an error if it find 0 or >1 record.  can be useful
            data recordToUpdate = db.datas.Where(d => d.data_id == id).Single();

            //change data in the record instance.  Remember that this record is tied to the db instance of the database.  No other instance contains this change
            recordToUpdate.email_address = newData;

            try
            {
                //you could leave out the try and catch the error higher up
                db.SubmitChanges();
            }
            catch
            {
                //notify? raise error?
            }
  //once db loses scope the changes vanish with it
}


Just remember that with link to sql, no actual changes happen until you try to submit the changes to that db instance
 
Share this answer
 
If I understood the question correctly, you should use the TransactionScope Class[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900