| Using customer As Customers = New Customers() |
| ' If we want multiple objects to use the same transaction we need to set it the first thing. |
| customer.UseTransaction = True |
| |
| ' If we set the ObjectMode to save, any joins we create will automatically receive the ObjectMode as well. |
| customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Save |
| |
| ' Now the following call will make sure that the "Orders" object also is using the same connection and transaction objects. |
| customer.Join_CustomerID_Orders_Child() |
| ' The following call will also share the same connection and transaction with "OrderITems" object as well. |
| customer.CustomerID_Orders_Child.Join_OrderID_OrderItems_Child() |
| |
| ' Now just as an example we are going to use nested loops and we'll insert 20 customers |
| ' For each customer we'll insert 50 orders and for each order we'll insert 100 order items. |
| With customer |
| For customerCounter As Integer = 21 To 25 |
| ' Assign values to the customer's fields |
| .FirstName.Value = "C" + customerCounter.ToString() |
| .LastName.Value = "C" + customerCounter.ToString() |
| If .Insert Then |
| With .CustomerID_Orders_Child |
| For orderCount As Integer = 0 To 50 |
| ' Assign values to the order's fields |
| .CustomerID.Value = customer.CustomerID.Value |
| .OrderAmount.Value = orderCount * 100 |
| .OrderDate.Value = DateTime.UtcNow.AddDays((orderCount * -1)) |
| .OrderProcessed.Value = False |
| If .Insert Then |
| With .OrderID_OrderItems_Child |
| For orderItemCount As Integer = 0 To 100 |
| .OrderID.Value = customer.CustomerID_Orders_Child.OrderID.Value |
| .Quantity.Value = orderItemCount |
| .ItemAmount.Value = orderCount * orderItemCount |
| .ProductID.Value = 1 |
| If Not .Insert Then |
| .RollBackTransaction() |
| MessageBox.Show("Ooops the order was not inserted. Error: " + .ErrorString) |
| Stop |
| End If |
| Next |
| End With |
| Else |
| .RollBackTransaction() |
| MessageBox.Show("Ooops the order was not inserted. Error: " + .ErrorString) |
| Stop |
| End If |
| Next |
| End With |
| Else |
| .RollBackTransaction() |
| MessageBox.Show("Ooops the customer was not inserted. Error: " + customer.ErrorString) |
| Stop |
| End If |
| Next |
| End With |
| ' If we reached so far that indicates that there were no errors and we can commit the transaction |
| ' You can use any other way.. perhaps keep a Boolean variable that tracks the success of all the inserts |
| ' And if nothing failed only then call commit else call the rollback |
| If Not customer.DBTransaction Is Nothing Then |
| MessageBox.Show("Everything looks good time to commit") |
| ' You can call the Commit or Rollback on any of the joined objects as long as you used the UseTransaction before creating joins |
| ' This ensures that all the objects used the same transaction and connection and hence when you commit or rollback on any of the objects it has the same affect for every object. |
| customer.CommitTransaction() |
| |
| ' Or just to see if Rollback works you can uncomment the line below and comment out the commit |
| 'customer.RollBackTransaction() |
| End If |
| |
| End Using |
| |