如何在C#中使用DataAdapter和存储过程对数据访问进行事务控制?
如何在C#中使用DataAdapter和存储过程对数据访问进行事务控制?目前我想通过DataAdapter执行2个存储过程调用,但我想对其进行事务控制。有什么办法可以做到吗?
How to do transaction control on data access using DataAdapter and Stored Procedure in C#? Currently I want to execute 2 stored procedure calls via DataAdapter, but I want to do transaction control on it. Is there any way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做的首选方法是使用事务范围来为您处理此问题。只需用新的
TransactionScope
:同一连接上发生的对数据库的任何调用将自动合并为单个交易。您还可以指定事务范围内的代码是否应加入现有事务或通过可选的
TransactionScopeOption
参数。这是将调用合并到单个事务中的首选方式。 替代方案是通过调用
Connection.BeginTransaction()
- 执行您的工作,然后调用tran.Commit()
。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
: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.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 callingtran.Commit()
.我相信最简单的方法是将这两个调用包装在 TransactionScope 中。请参阅本页上的示例:
http://msdn.microsoft。 com/en-us/library/system.transactions.transactionscope.aspx
I believe that the easiest way to do this would be to wrap both calls in a TransactionScope. See the example on this page:
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx