简体   繁体   English

如何处理.net对象中的事务?

[英]How to handle transactions in .net objects?

I am not talking about transactions at database level. 我不是在谈论数据库级别的事务。 I woukld like to implement transactions at object level. 我喜欢在对象级别实现事务。 Here is my code: 这是我的代码:

WorkQueue wq = new WorkQueue();
wq.OWNER_ID = user.EmployeeId;
wq.OWNER_NM = user.FullName;
wq.WQ_DETAILS = txtDetails.Text;
wq.SOURCE_INFO = txtSource.Text;
wq.PRIORITY_ID = Convert.ToInt32(ddlRequestType.SelectedItem.Value);
wq.PRO_ID = Convert.ToInt32(ViewState["pro_id"]);
wq.GROUP_ID = Convert.ToInt32(ViewState["cat_id"]);
wq.Save();

WQAttachment attachment = new WQAttachment();
attachment.ATTACH_SIZE = FileUpload1.PostedFile.ContentLength.ToString();
attachment.ATTACH_FILE = FileUpload1.FileBytes;
attachment.WQ_ID = wq.WQ_ID.Value;
attachment.ATTACH_FILE_NM = FileUpload1.FileName;
attachment.ATTACH_NM = "Upload test";
attachment.ATTACH_TYPE = FileUpload1.PostedFile.ContentType;
attachment.Save();

Is there a way I can wrap this in a transaction so if attachment.save fails, it should roll back wq.save. 有没有一种方法可以将其包装在事务中,因此,如果attachment.save失败,它应该回滚wq.save。 Thanks! 谢谢!

I see a SO answer regarding TransactionScope, Can I use this for my case above? 我看到一个关于TransactionScope的SO答案,我可以在上面的案例中使用它吗? Transactions in .net .net中的交易

System.TransactionScope works great for this scenario. System.TransactionScope在这种情况下非常有用。 Thanks @Aaron and @JohnSaunders 谢谢@Aaron和@JohnSaunders

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM