I want to cancel a transaction named "scope", but an internal transaction named scope2 should not rollback. But they both roll back !!! TransactionScopeOption.Suppress option does not work ...
I have already enabled DTC and I am using Visual Studio 2010 with .net 4.0 and Microsoft SQl Server 2008. What is wrong here ???
using (SqlConnection conn = new SqlConnection(@"Data Source=.\DEVELOPER;Initial Catalog=TestDatenbank;Integrated Security=sspi")) { using (TransactionScope scope = new TransactionScope()) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "UPDATE Orders SET ID='111'"; cmd.ExecuteNonQuery(); using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.Suppress)) { SqlCommand cmd1 = new SqlCommand(); cmd1.Connection = conn; cmd1.CommandText = "UPDATE Orders SET OrderName='aaa'"; cmd1.ExecuteNonQuery(); scope2.Complete(); }
Thank you for your responses!
I finally work !!!!
using (SqlConnection conn = new SqlConnection(@"Data Source=.\DEVELOPER;Initial Catalog=TestDatenbank;Integrated Security=sspi")) { using (TransactionScope scope = new TransactionScope()) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "UPDATE Orders SET ID='111'"; cmd.ExecuteNonQuery(); using (SqlConnection conn2 = new SqlConnection(@"Data Source=.\DEVELOPER;Initial Catalog=AdventureWorks;Integrated Security=sspi")) { using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.Suppress)) { conn2.Open(); SqlCommand cmd1 = new SqlCommand(); cmd1.Connection = conn2; cmd1.CommandText = "UPDATE Henrik SET ID='111'"; cmd1.ExecuteNonQuery(); scope2.Complete(); } }
One strange thing is when I delete the line scope2.Complete (); and replace //scope.Complete (); with scope.Complete (); I expected the following:
area will be executed
scope2 will lean back
but actually: the scope will be executed scope2 will also be executed
Any ideas ???????
source share