达尔的交易问题 [英] transaction problem in dal

查看:65
本文介绍了达尔的交易问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用数据访问层

现在我使用相同的功能在三个表中插入记录,那么我如何使用事务来处理任何表的任何故障

我的代码就像......



i am using data access layer for my project
now i am inserting records in three tables using the same function so how can i use transaction for any failure of any table
my code like...

int check2 = du.ExecuteSqlSP(objAdTask, spProjectTask);

            int check = du.ExecuteSqlSP(objaddSubAdmin, spAdmin);
            int check1 = du.ExecuteSqlSP(objAdProject, spAdminProject);





其中obj ..是参数而sp ..是我的storedProcedure名称

如何使用上述代码的事务



where obj.. are parameters and sp.. is my storedProcedure name
how to use transaction for above code

推荐答案

那里这是一种优雅的方式。

使用该交易。启动一个事务,把你的存储过程调用在try catch块中。执行存储过程后提交事务。 catch块中的其他回滚。



所以它会像

There is an elegant way to do this.
Use the transaction for that. start a transaction, put your stored procedures call in try catch block. After executing the stored procedures commit the transaction. else rollback in catch block.

So it would be something like
IDbConnection conn = //(Create your connecttion object here)
IDbTransaction transaction = conn.BeginTransaction();
try{
       //(Execute you procedures here)
        transaction.Commit();
}
 catch
 {
  // Roll back the transaction. 
  transaction.Rollback();
  throw; 
 }


这篇关于达尔的交易问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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