How do I do transactions in .Net ?

  • Thread starter Thread starter Guest
  • Start date Start date
<nothing>

It would help if you'd be more specific in your question. For most
database connections, however, there's a BeginTransaction method call
you can make which will return you a reference to an object
representing the transaction - that can then be committed or rolled
back.
 
Yaron,

like Jon said, these newsgroups probably aren't the best place to learn
broad topics like transaction handling, but I can give you pointers where
to start. Also, the term "transaction" can also be used in many different
contexts in programming; however I assume you are looking for information
regarding database (ADO.NET) transactions.

In ADO.NET, transactions are started with a method from the connection
class. For example, if you were accessing SQL Server, then you would use the
System.Data.SqlClient.SqlConnection class. This class has a method called
BeginTransaction which starts a transaction. This returns an object of type
SqlTransaction. Other ADO.NET connection classes like OleDbConnection has a
similar method.

After you are done, you can call the Commit method of the given
SqlTransaction object, or Rollback should you want to roll the transaction
back.

The .NET Framework SDK documentation has good information about
transactions. See the index entry "transaction processing, about transaction
processing" and the related topics form more information.

You might also find Shrijeet Nair's quick introduction helpful (from C#
Corner):
http://www.c-sharpcorner.com/Code/2002/Aug/TransactionsNConcurr.asp

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
You can use the BeginTransaction() method of the appropriate DB connection
object and call Commit() or Rollback() method on the returned transaction
object reference.

Hope this is what you are looking for.

news:[email protected]...
 
Yaron,

Depending on your needs, you might not want to do what the previous
posters suggested. If your system is large enough, and you have multiple
objects that will be participating in the outcome of the transaction (or
even other transactional resources), then managing the transaction state
yourself is going to be difficult, to say the least. For things like this,
I would recommend creating a component (or components) with your logic that
derive from ServicedComponent and use the COM+ environment to handle your
transactions for you. Most transactional resources will know that they are
in the appropriate environment and act accoringly when the transaction is
committed/aborted.

In .NET 2.0, you can also use the classes in the System.Transaction
namespace (most notably the TransactionScope class) to define the
transactional boundaries of your application. Again, resources that are
able to be managed by a transaction manager will auto-enroll in the
transaction when there is one present (this includes database connections,
MSMQ messages, and the file system, in the future (longhorn)).

All in all, I wouldn't even try to manage the transaction state
yourself. The moment you have multiple objects that have to participate, or
multiple resources participating in the transaction, managing that yourself
becomes a nightmare.

Hope this helps.
 
Back
Top