C#事务和存储过程rollbcak [英] C# transaction and store procedure rollbcak

查看:184
本文介绍了C#事务和存储过程rollbcak的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有三个表,一个掌握一个细节,另一个掌握.
INVOICE_MASTER,INVOICE_DETAIL,PRODUCT_QUNTY.现在我有两个存储过程. sp1在INVOICE_MASTER和UPDATE PRODUCT_QUNTY中进行INSERT,而sp2在INVOICE_DETAIL和UPDATE PRODUCT_QUNTY中进行INSERT.我在所有sp中都使用transaction,commit和rollback.现在,我使用来自应用程序的事务(在C#中)执行那些过程.现在我的问题是,如果回滚发生在c#事务中,那么所有这些事务都是sp,是否会回滚?请提出如何进行此类事务的方法,以寻求适当的解决方案.

Suppose I have three table ,one master one detail and other one.
INVOICE_MASTER, INVOICE_DETAIL, PRODUCT_QUNTY. Now I have two store procedure. sp1 which INSERT in INVOICE_MASTER and UPDATE PRODUCT_QUNTY and sp2 INSERT in INVOICE_DETAIL and UPDATE PRODUCT_QUNTY. I use transaction,commit and rollback in all sp. Now I use transaction from application (in c#) to execute those procedure. Now my question is, if rollback occurred in c# transaction then all those transaction is sp , will be rollback or not? Please suggest how to do this type of transaction for proper solution.

推荐答案

事务是RDBMS概念,尽管您可以通过c#代码进行设置.这样,如果在C#中回滚事务,同样也会影响sp.s.

换句话说,您可以在sp,DAL(ADO.net),linq等中定义事务范围.但是在这些地方,您可以定义实际上属于RDBMS的事务范围.

要在C#中使用,只需使用:

Transaction is RDBMS concept though you can set it through c# code. In this way, if you rollback your transaction in C#, the same will affect in sp''s as well.

In other words you can define transaction scope in sp, DAL (ADO.net), linq etc. But these are the places where you can define your transaction scope which actually belongs to the RDBMS.

To use in C# simply use:

using (TransactionScope scope = new TransactionScope())
        {
// your code
}



使用BeginTrans在sql中启动事务作用域.请在谷歌上搜索这些概念.



Use BeginTrans to start transaction scope in sql. Pls google around these concepts.


由于它们是有效的嵌套事务,因此C#代码中的任何回滚都将覆盖SP中的提交.
因此,如果出于任何原因在C#中回滚,则SP事务将被丢弃.

拥有这样的多个事务没有问题:您的许多开发人员都不需要了解SP的内部工作原理以及它是否完全包含事务.交易可能由于完全不同的原因而回滚.
Because they are effectively nested transactions, any rollback in the C# code will override a commit in the SP.
So if you rollback for any reason in the C#, the SP transactions will be discarded.

There is no problem with having multiple transactions like this: many of your developers do not need to know the inner workings of the SP and whether it contains a transaction at all. The transactions could well be rolling back for completely different reasons.


这篇关于C#事务和存储过程rollbcak的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆