transaction
1.在執行中間有出錯,所有的刪除都會返回原始狀態
- string str1 = @"DELETE LineItem where OrderId = @OrderId;"; // SQL command 1
- string str2 = @"DELETE OrderStatus where OrderId = @OrderId;"; // SQL command 2
- string str3 = @"DELETE Orders where OrderId = @OrderId;"; // SQL command 2
- SqlConnection conn = NEW SqlConnection(SqlHelper.ConnectionStringOrderDistributedTransaction);
- SqlCommand delCommand = NEW SqlCommand(str1, conn);
- SqlParameter param = NEW SqlParameter("@OrderId", 2);
- delCommand.Parameters.ADD(param);
- conn.OPEN();
- SqlTransaction trans = conn.BeginTransaction();
- delCommand.TRANSACTION = trans;
- try
- {
- delCommand.ExecuteNonQuery();
- delCommand.CommandText = str2;
- delCommand.CommandType = CommandType.Text;
- delCommand.ExecuteNonQuery();
- delCommand.CommandText = str3;
- delCommand.CommandType = CommandType.Text;
- delCommand.ExecuteNonQuery();
- trans.Commit();
- } // try
- catch (Exception excep)
- {
- trans.ROLLBACK(); // 出現例外就ROLLBACK
- logger.Debug(excep.Message);
- } // catch
- finally
- {
- conn.Close();
- delCommand.Dispose();
- conn.Dispose();
- trans.Dispose();
- } // finally
沒有留言:
張貼留言