// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Data; using System.Data.Common; using System.Transactions; namespace Tx.NonXADB.ADONET { public class Smart : DB { public override IDbConnection GetConnection() { IDbConnection con = DbProviderFactories.GetFactory("MySql.Data.MySqlClient").CreateConnection(); con.ConnectionString = "Server=localhost;Database=Test;User Id=root;Password=hemmeligt"; con.Open(); return con; } public override void Test() { for(int i = 0; i < 10; i++) { using(TransactionScope tx = new TransactionScope()) { using(IDbConnection con = GetConnection()) { try { using(IDbCommand cmd = con.CreateCommand()) { cmd.Connection = con; cmd.CommandText = "UPDATE tblsrc SET val = val - 1 WHERE id = 1"; cmd.ExecuteNonQuery(); if(i % 2 != 0) throw new Problem(); cmd.CommandText = "UPDATE tbltrg SET val = val + 1 WHERE id = 1"; cmd.ExecuteNonQuery(); } tx.Complete(); } catch(Problem) { Console.WriteLine("Problem"); } } } } } public static void Main(string[] args) { Demo(new Smart()); Console.ReadKey(); } } }