Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press
Buy directly from Paradoxal Press at $33.99 (Save 43%)
Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA
|
Listing 20-6 extracted from chapter Transactions
Listing 20-5< > Listing 20-7
This listing can be compiled with the command line: csc.exe /target:exe Example_20_6.cs /r:System.Transactions.dll Errors: 0 Warnings: 0
Example_20_6.cs
using System.Data.Common;
using System.Data.SqlClient;
using System.Transactions;
using System.Threading;
class Program {
static string sCnx =
"server = localhost ; uid=sa ; pwd =; database = ORGANIZATION";
static string sCmd1 =
"INSERT INTO DEPARTMENTS VALUES ('COM','Communication')";
static string sCmd2 =
"INSERT INTO EMPLOYEES VALUES ('COM','Smith','Adam','(123) 456-7899')";
static void Main() {
try {
using (TransactionScope txScope = new TransactionScope()) {
using (SqlConnection cnx = new SqlConnection(sCnx)) {
cnx.Open();
DbCommand cmd = new SqlCommand(sCmd1, cnx);
cmd.ExecuteNonQuery();
DependentTransaction depTx =
Transaction.Current.DependentClone (
DependentCloneOption.BlockCommitUntilComplete );
ThreadPool.QueueUserWorkItem( AsyncProc, depTx);
txScope.Complete();
} // end using cnx.
} //end using txScope 'ITransaction.Commit()' is called here.
} catch { }
}
static void AsyncProc(object state) {
DependentTransaction depTx = state as DependentTransaction;
try {
using ( SqlConnection cnx = new SqlConnection( sCnx ) ) {
cnx.Open();
cnx.EnlistTransaction( depTx );
DbCommand cmd = new SqlCommand( sCmd2, cnx );
cmd.ExecuteNonQuery();
depTx.Complete();
} // end using cnx.
} catch {
depTx.Rollback();
}
}
}
Copyright Patrick Smacchia 2006 2007
|