PC Review


Reply
Thread Tools Rate Thread

Access DB transaction or rollback?

 
 
Sin Jeong-hun
Guest
Posts: n/a
 
      17th Dec 2006
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.

 
Reply With Quote
 
 
 
 
Rad [Visual C# MVP]
Guest
Posts: n/a
 
      17th Dec 2006
On 17 Dec 2006 07:53:53 -0800, Sin Jeong-hun wrote:

> 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.
--
Bits.Bytes
http://bytes.thinkersroom.com
 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      18th Dec 2006
I remembered this article:
http://www.codeproject.com/dotnet/ELAB.asp

i never tested it / used it, but you might check it out.


"Sin Jeong-hun" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Sin Jeong-hun
Guest
Posts: n/a
 
      18th Dec 2006
Thank you. I'll try it.

Rad [Visual C# MVP] wrote:
> On 17 Dec 2006 07:53:53 -0800, Sin Jeong-hun wrote:
>
> > 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.
> --
> Bits.Bytes
> http://bytes.thinkersroom.com


 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      18th Dec 2006
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.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Sin Jeong-hun" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
transaction and rollback Souris Microsoft Access Queries 4 24th Jan 2008 11:38 AM
Transaction.Commit() and Transaction.Rollback() weird0 Microsoft C# .NET 5 3rd Nov 2007 01:39 PM
Access Transaction Rollback problem =?Utf-8?B?Q29saW4=?= Microsoft Access VBA Modules 12 22nd Aug 2007 09:36 AM
commit or rollback a transaction without first beginning a transaction david epsom dot com dot au Microsoft Access VBA Modules 17 25th Feb 2005 01:22 PM
Access Transaction Rollback causes Recordset cursor loss Bryce Powell Microsoft Access Form Coding 0 29th Sep 2003 11:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:21 PM.