using trigger in C# Windows Form

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

MyForm is a C# Windows Form.
MyFom has 3 GroupBoxes.
Now I wanna update data from the textboxes in those 3 GroupBoxes.
I have function Update1 for GroupBox1 to update table Table1, Update2 for
GroupBox2 to update table Table2, Update3 for GroupBox3 to update table
Table3.
And there is a function TotalUpdate for MyForm.
The function TotalUpdate comprises of Update1, Update2, and Update3.
Now my question is how do I RollBack the Update1 if the Update3 fails?
How do I incurr a trigger in my C# code?
Thanks for help.


Jason
 
Use a Transaction.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
Thanks Kevin.
But I am wondering how a transaction can be split into 3 functions and works
as a single transaction?
 
Transactions can happily span multiple DB requests. The traditional way
would be to create an ADO transaction and hand it around for objects
(calling to the database) to enlist within, but unless you own all the code
performing the changes it can be hard to do this.

However, under 2.0 (and especially if you are using SQL-Server 2005) most
(all?) inbuilt database calls respect the new TransactionScope entity - this
creates an "ambient transaction" which can work (broadly: read up on MSDN2
for more info) as an ADO transaction or a DTC transaction, yet requires very
little work: in particular, you don't need to explicitely pass the
transaction to all of the callers.

Example (note: you need to reference System.Transactions and add an "using"
clause)

using(TransactionScope transaction = new TransactionScope()) {
SomeMethodThatUpdatesTheDatabase();
SomeOtherMethodThanUpdatesTheDatabase();
YouveGuessedItIAlsoUpdateTheDatabase();
transaction.Compete();
}

If anything throws an exception, transaction is disposed without ever
calling Complete(); this causes the transaction to rollback, and your
changes are undone. If Complete() *is* called. the transaction is committed.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top