简体   繁体   English

如何使用C#中的DataAdapter和存储过程对数据访问进行事务控制?

[英]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.

相关问题 如何使用DataAdapter通过可变参数在C#中调用存储过程 - How to use DataAdapter to call a stored procedure in C# with variable parameters 如何在C#中使用sql dataadapter进行内联查询作为存储过程 - how to make in line query as stored procedure with sql dataadapter in c# 使用存储过程操作数据,使用 C# 进行事务 - manipulating data with Stored Procedure, transaction using c# 如何在一个存储过程中针对具有不同类型的表SQL Server和C#的同一数据库执行多个事务? - How to do multiple transaction in one stored procedure for the same data base with different kind of tables SQL sever & C#? 如何使用C#中的存储过程在Oracle数据库中插入数据? - How to insert data in oracle database using stored procedure in c#? 使用DataAdapter无法运行存储过程 - Fail to run stored procedure using DataAdapter 如何使用带有存储过程和参数的 DataAdapter - How to use a DataAdapter with stored procedure and parameter 如何使用C#检索存储过程? - How to retrieve a stored procedure using c#? 使用c#和MySql存储过程(在数据访问层MVC中)找出Last_Insert_Id() - Figure out Last_Insert_Id() Using c# and MySql stored Procedure (in Data Access Layer MVC ) 您是否需要一个DataTable来从DataAdapter检索数据? - c# - Do you need a DataTable to retrieve data from a DataAdapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM