Access DB transaction or rollback?

  • Thread starter Thread starter Sin Jeong-hun
  • Start date Start date
S

Sin Jeong-hun

Hi.
I wrote a C# program that uses System.Data.OleDb (MS Access). For a
certain operation, it calls bunch of ExecuteScalar(). The problem is,
if any of those ExecuteScalar failes, then the whole DB becomes
unstable data. So can I set a rollback point or group them in a one
transaction?
Some pseudo-code like,
try
{
MyCommand.SetRollbackPoint();
MyCommand.ExecuteScalar();
MyCommand.ExecuteScalar();
......
MyCommand.ExecuteScalar();
}
catch
{
MyCommand.Rollback();
}

-------OR----------
MyCommand.StartTransaction();
MyCommand.ExecuteScalar();
MyCommand.ExecuteScalar();
......
MyCommand.ExecuteScalar();
MyCommand.EndTransaction();

Any suggestions will be welcome! Thank you.
 
Hi.
I wrote a C# program that uses System.Data.OleDb (MS Access). For a
certain operation, it calls bunch of ExecuteScalar(). The problem is,
if any of those ExecuteScalar failes, then the whole DB becomes
unstable data. So can I set a rollback point or group them in a one
transaction?
Some pseudo-code like,
try
{
MyCommand.SetRollbackPoint();
MyCommand.ExecuteScalar();
MyCommand.ExecuteScalar();
.....
MyCommand.ExecuteScalar();
}
catch
{
MyCommand.Rollback();
}

-------OR----------
MyCommand.StartTransaction();
MyCommand.ExecuteScalar();
MyCommand.ExecuteScalar();
.....
MyCommand.ExecuteScalar();
MyCommand.EndTransaction();

Any suggestions will be welcome! Thank you.

Most, if not all database providers provide a Transaction object e.g.
OleDbTransaction, SqlTransaction, OdbcTransaction that you can use
precisely for this sort of thing.

This object provides commit and rollback methods that do what you're
looking for.
 
Thank you. I'll try it.
Most, if not all database providers provide a Transaction object e.g.
OleDbTransaction, SqlTransaction, OdbcTransaction that you can use
precisely for this sort of thing.

This object provides commit and rollback methods that do what you're
looking for.
 
Sin Jeong-hun,

I think you really should look into the TransactionScope class in the
System.Transactions namespace. It will allow you to create a scope of code
where there is a transaction, and you can call Commit at the end of the code
to commit all the changes. If you don't, then everything is automatically
rolled back.

Hope this helps.
 
Back
Top