如何将交易用于拖车桌? [英] How can I use transaction for tow table?

查看:78
本文介绍了如何将交易用于拖车桌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将交易用于拖车桌?

例如我们有两个表(主要和细节),当我在主表中插入一个记录时,可以用for循环(超过一个记录)详细插入一些记录。我需要按时交易控制牵引表。

How can I use transaction for tow table?
For example we have tow table (main and detail), when I insert one record in main table, may insert SOME record in detail with for loop(MORE THAN ONE RECORD). I need control tow table with transaction on time.

推荐答案

是的,您可以设置交易以插入/更新表格超过1

1。为此,你必须在列表中添加详细对象

2.一次发送父对象和详细信息对象给BLL

3.在BLL申请交易

4.这将把交易应用于父母以及细节交易
yes you can set transaction for inserting/updating tables more than 1
1. For that you have to add detail objects in list
2. Send parent and details object at a time to BLL
3. In BLL apply transaction
4. This will apply transaction to parent as well as details transacations


你可以做这样的事情......



1)创建连接

2)创建交易

3)使用该交易插入主记录(但不提交)

4)插入详细信息记录

5)如果一切顺利,那么提交事务或者发生错误回滚事务。



像这样......





you can do something like this...

1) create connection
2) create transaction
3) insert master record using that transaction (but do not commit it)
4) insert details record(s)
5) if all the thing is going good then commit the transaction or if error occurred rollback transaction.

something like this...


SqlConnection db = new SqlConnection("connstringhere");
      SqlTransaction transaction;

      db.Open();
      transaction = db.BeginTransaction();
      try 
      {
         new SqlCommand("INSERT INTO master " +
            "(Text) VALUES ('Row1');", db, transaction)
            .ExecuteNonQuery();
         for(int i =0; i<10;i++){
         new SqlCommand("INSERT INTO details VALUES " +
            "( " + i + ");", db, transaction)
            .ExecuteNonQuery();
           }
         transaction.Commit();
      } 
      catch (SqlException sqlError) 
      {
         transaction.Rollback();
      }
      db.Close();


这篇关于如何将交易用于拖车桌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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