Using Transactions
Posted on: Fri Jun 27 12:26:12 -0700 2008. Updated on: Thu Sep 04 12:45:26 -0700 2008.
Category: DotNet
Category: DotNet
It is important to use transactions when inserting or doing a lot of database manipulation. Here is a quick example:
using (TransactionScope scope = new TransactionScope())
{
SomeDSTableAdapters.cs_recordsTableAdapter ta = new SomeDSTableAdapters.cs_recordsTableAdapter();
ta.Connection.ConnectionString = _connectionString;
ta.DeleteAllRecords();
ta.Update(ds);
scope.Complete();
}
To Enable transactions go to "References" (in your project), right click and hit "Add Reference" and scroll down to System.Transactions and hit Ok. Next add the following code to the very top of your cs file:
using System.Transactions;