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

时间:2014-01-16 14:29:32

标签: c# transactions

我不是在谈论数据库级别的事务。我想在对象级别实现事务。这是我的代码:

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();

有没有办法可以在事务中包装它,所以如果attachment.save失败,它应该回滚wq.save。谢谢!

我看到有关TransactionScope的SO答案,我可以在上面的案例中使用它吗? Transactions in .net

1 个答案:

答案 0 :(得分:0)

System.TransactionScope适用于此方案。谢谢@Aaron和@JohnSaunders