如何设置 Linq to SQL 的保存点并使用“NO”执行命令?
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 一起使用。我想使用一个函数,因为我看不到保存点中发生了什么!
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();
}
It works, but only with ExecuteCommand. I want to use a function, because i can't see what happens in the savepoint !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议根本不要这样做。这不一定是您想听到的,但是尤其在与
TransactionScope
混合时,保存点并不是一个好主意。 TransactionScope 可以嵌套,但是第一次回滚就注定了一切,并且提交仅发生在最外层事务。在我能想到的大多数情况下,最好首先清理数据。您还可以(并且应该)使用安全网的约束,但如果您触及该安全网,则假设出现大问题并回滚所有内容。
嵌套交易示例:
在上面,
DebitCreditAccount
是原子的 - 我们将添加帐户交易并更新估计余额,或者两者都不更新。如果这是唯一事务,那么它将在此方法结束时提交。然而,在 Transfer 方法中,我们创建了另一个外部事务;我们要么两者都执行
DebitCreditAccount
,要么都不执行。此处,内部tran.Complete()
(在DebitCreditAccount
中)不会提交数据库事务,因为存在外部事务。它只是说“我很高兴”。相反,如果任一内部事务被中止(在没有Complete()
的情况下调用Dispose()
),则外部事务 事务立即回滚,并且该事务将拒绝任何额外的工作。仅当没有内部事务被中止,且并且在外部调用Complete()
时,外部事务才会提交交易。I would advise simply not to. It isn't necessarily what you want to hear, but especially when mixing with
TransactionScope
, save-points aren't a great idea.TransactionScope
s can be nested, but the first rollback dooms everything, and the commit only happens at the outermost transaction.In most scenarios I can think of, it is better to sanitise the data first. You can (and should) also use contraints for a safety net, but if you hit that safety net, assume big problems and rollback everything.
Example of nested transactions:
In the above,
DebitCreditAccount
is atomic - we'll either add the account-transaction and update the estimated balance, or neither. If this is the only transaction, then it is committed at the end of this method.However, in the
Transfer
method, we create another outer transaction; we'll either perform bothDebitCreditAccount
, or neither. Here, the innertran.Complete()
(inDebitCreditAccount
) doesn't commit the db-transaction, as there is an outer transaction. It simply says "I'm happy". Conversely, though, if either of the inner transactions is aborted (Dispose()
called withoutComplete()
), then the outer transaction is rolled back immediately, and that transaction will refuse any additional work. The outer transaction is committed only if no inner transaction was aborted, andComplete()
is called on the outer transaction.执行查询怎么样?
使用 DataContext.ExecuteQuery,您可以将文本发送到数据库中,就像 ExecuteCommand 一样 - 但您可以从该文本中获取查询结果。
How about ExecuteQuery?
With DataContext.ExecuteQuery, you send text into the database, just like ExecuteCommand - but you can get query results back from that text.