如何在linq中使用提交回滚 [英] how to use commit rollback in linq

查看:140
本文介绍了如何在linq中使用提交回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是linq的新手,我想使用c#4.0在linq中使用提交回滚tsql

I an new in linq i want to use commit rollback tsql in linq using c# 4.0

推荐答案

在数据库上发生任何更改之前,您必须具有数据库.submitchanges()提交更改之前.同时发生的任何事情都将保留在交易中. :

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. :

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
}



只需记住,使用sql链接,直到您尝试将更改提交到该数据库实例之前,才发生实际更改



Just remember that with link to sql, no actual changes happen until you try to submit the changes to that db instance


如果我正确理解了该问题,则应使用TransactionScope类 [
If I understood the question correctly, you should use the TransactionScope Class[^]


这篇关于如何在linq中使用提交回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆