【发布时间】:2010-06-16 12:00:08
【问题描述】:
TransactionScope TransactionABC = new TransactionScope();
try
{
context.Connection.Open();
{
context.ExecuteCommand("insert into test (test) values (1)")
context.SubmitChanges();
context.ExecuteCommand("savepoint test");
context.ExecuteCommand("insert into test (test) values (2)")
context.SubmitChanges();
context.ExecuteCommand("rollback to test");
}
TransactionABC.Complete();
TransactionABC.Dispose();
}
catch (Exception ec)
{
MessageBox.Show(" ", ec.Message);
}
finally
{
context.Connection.Close();
}
它有效,但仅适用于 ExecuteCommand。我想使用一个函数,因为我看不到保存点中发生了什么!
【问题讨论】:
-
你真的想成为
using事务范围。否则,当您遇到异常时,您将交易保持打开状态 - 这很糟糕。它应该是using(var tran = new TransactionScope()) { /* code */ tran.Complete(); }- 然后在成功和失败退出时正确处理它,只有成功调用Complete()。
标签: c# linq-to-sql transactions savepoints