Child Parent Transactions roll back

后端 未结 2 1777
遇见更好的自我
遇见更好的自我 2021-01-25 15:13

I have a scenario in which I have to process multiple .sQL files, every file contains 3-4 insert or Update queries, now when any query in a file fails I do ro

2条回答
  •  长情又很酷
    2021-01-25 15:50

    Keep all insert, update queries in a try{..}catch(..){..} and if any exception occurs, in the catch roll the db transaction back.

         private void InsertFoo(SqlTransaction txn, string fooName)
         {
             using (var cmd = txn.Connection.CreateCommand())
             {
                 try
                 {
                     do your process here...
                     cmd.Commit();
                 }
                 catch(Exception ex)
                 {
                    cmd.Rollback();
                 }
             }
         }
    

提交回复
热议问题