简体   繁体   中英

.NET 4.0 transaction behavior when calling stored procedure

If I create a SqlConnection and do not create a SqlTransaction for it from C# code and then use a SqlCommand to invoke a stored procedure, what is the transactional behavior of the code in the following two scenarios

  1. Stored procedure does not have any BEGIN TRANSACTION statements
  2. Stored procedure has a single BEGIN TRANSACTION at start and ends with a COMMIT TRANSACTION .

Assume that no errors occur that would cause the transactions to rollback

From what I understand, the stored procedure would be invoked outside a transaction and cause the following behavior:

  1. Each statement in the stored procedure would execute in its own transaction
  2. The transaction opened in the stored procedure would be the top level transaction and all statements in the stored procedure would be associated with that transaction.

Please comment if my understanding in scenarios in A and B is correct.

Your understanding of the scenarios of A and B is correct.

People prefer ADO.NET transaction when lot of UI side data interaction is involved and people prefer to tackle it at UI Side. Otherwise you have to pass on all the data to the SQL Side and let it handle it which sometimes becomes a overhead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM