[英]How to do transaction control on data access using DataAdapter and Stored Procedure in C#?
How to do transaction control on data access using DataAdapter and Stored Procedure in C#? 如何使用C#中的DataAdapter和存储过程对数据访问进行事务控制? Currently I want to execute 2 stored procedure calls via DataAdapter, but I want to do transaction control on it.
目前我想通过DataAdapter执行2个存储过程调用,但我想对它进行事务控制。 Is there any way to do it?
有什么办法吗?
The preferred way of doing so is to use transaction scopes to handle this for you. 这样做的首选方法是使用事务范围来为您处理此问题。 Just surround the body of code that invoke both stored procedure calls with a new
TransactionScope
: 只需使用新的
TransactionScope
包围调用两个存储过程调用的代码体:
using(TransactionScope scope = new TransactionScope())
{
// your ADO.NET code that calls sprocs ...
}
Any calls to the database that occur on the same connection will automatically be combined into a single transaction. 在同一连接上发生的对数据库的任何调用都将自动合并为一个事务。 You can also specify whether the code within the transaction scope should enlist in an existing transaction or start its own via the optional
TransactionScopeOption
parameter. 您还可以指定事务范围内的代码是应该在现有事务中登记还是通过可选的
TransactionScopeOption
参数启动它自己的TransactionScopeOption
。
This is the preferred manner of combining calls into a single transaction. 这是将调用组合到单个事务中的首选方式。 The alternative , is to manually acquire a DBTransaction by calling
Connection.BeginTransaction()
- performing your work and then calling tran.Commit()
. 另一种方法是通过调用
Connection.BeginTransaction()
手动获取DBTransaction - 执行您的工作然后调用tran.Commit()
。
I believe that the easiest way to do this would be to wrap both calls in a TransactionScope. 我相信最简单的方法是在TransactionScope中包装两个调用。 See the example on this page:
请参阅此页面上的示例:
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.