Transaction issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm inserting info in database when clicking a button. Some procedure are
called. I want to do that with a transaction (when clicking on the button
and within its procedure) so that everything will be
inserted or nothing. How to do that? Thanks

Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
addExtPartyListToDB()

AddReminderDB()

addCostSplitDB()

AddMoneyDB()

AddinfoDB()

AddmemberDB()
end sub
 
There's several ways to approach this. You could use COM+ (Enterprise
Services) and require transactions, but the simplest is to use the
SqlTransaction object:

Here's a brief excerpt. It assumes you are using SQL Server and
SqlDataAdapters.

// Create a SqlTranaction
SqlTransaction trans;
trans = myConnection.BeginTransaction();
try
{
// Assign the correct adapters and Commands to the transaction;
// These can be Update, Insert, or Delete Commands;
dataAdapter1.UpdateCommand.Transaction = trans;
dataAdapter2.InsertCommand.Transaction = trans;
dataAdapter3.DeleteCommand.Transaction = trans;

// Call the update methods for each adapter involved in the transaction;
dataAdapter1.Update(dataset);
dataAdapter2.Update(dataset);
dataAdapter3.Update(dataset);

// Commit the transaction;
trans.Commit();
}
catch(Exception e)
{
//If an exception occurs, then we need to rollback the transaction;
trans.Rollback();
}


Hope this helps.
Dave
 
You have some options:

1) Use ADO.NET to create a transaction and then pass it to each method so
that the method can use it to join the transaction

2) Create a "Transaction" class that inherits from the ServicedComponent
class in the Enterprise Services namespace and then use the "Transaction"
attribute to declare that you want the DTC (Distributed Transaction
Coordinator) to create a transaction for you. This "Transaction" class would
have a method that would in turn call the other methods and also act as the
root of the transaction.

3) This option uses enterprise services in a different way that you might
want to check out:
http://blogs.msdn.com/florinlazar/archive/2004/07/24/194199.aspx
 
Back
Top