rollback code

  • Thread starter Thread starter dmalhotr2001
  • Start date Start date
D

dmalhotr2001

Hi,

I have a piece of code that uploads a file and writes to a database.
Is there a way that if the upload of the file fails or the database
write fails, that the entire process can be rolled back, so that
neither was done.

Basically what I'm trying to do is kind of like a transaction in .net
but not exactly because its not all sql. I just want the entire
process to rollback to its original state if either the file upload or
the sql write fails.

Is there a code example online?

Thanks in advance.

:D
 
Since database already support transactions, you can open a transaction,
execute the SQL statements, upload the file and the upload fails rollback
the transaction. Otherwise, commit the changes.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Unless you do 2 things in 2 separate threads, just upload the file
first, then write to DB later. If either action goes wrong, rollback
the file upload action. The sql transaction will take care of rolling
back DB for you if uploading succeeded but updating DB failed.
 
Hi,

I have a piece of code that uploads a file and writes to a database.
Is there a way that if the upload of the file fails or the database
write fails, that the entire process can be rolled back, so that
neither was done.
<snip>

Transaction support is not supported in all systems. For instance, file
systems, file uploads/downloads, etc. is not transacted.

Instead you'll have to write code to react to the problems, for instance
by deleting the file you just uploaded, or similar.
 
how do you tell in c# that the file upload failed and if it did
rollback the transaction (if the transaction code is in sql stored
proc)?

How do I return the value of whether the tranaction has executed
properly to the code as well just in case I have to delete the file?

Thanks

:D
 
Hi dmalhotr2001,

Whenever I'm in the position in with trasactions are required but not
supported, I use a try construct.

Try all your actions but don't do them for real. If all succeeds do the
actions for real.

For instance uploading to multiple databases lets say a oracle and a sql
database.
Try the actions rollback by default. If all goes well (no errors) run the
actions again without the rollback.

In your case do the upload to a temp location, do the database actions and
rollback.
When both return without errors move the file to the real location and do
the databse actions again.

This is of course not as good as a transaction, but it increases the
likelyhood of a success.

Kind regards,
 
Back
Top