Hello
I am trying to run a program to check for transaction scope
I have written the following code.
But it seems I need to add a namespace or reference
What namespace am I supposed to add
thanks
using (TransactionScope scope = new TransactionScope(TransactionScope.Required, options))
{
SqlConnection MyCon = new SqlConnection("server=hemalatha\\sqlexpress;integr ated security=sspi;database=demo");
MyCon.Open();
SqlCommand Mycmd=new SqlCommand("insert into t1 values 574,'scope','10/10/2007',3,3,3");
Mycmd.ExecuteNonQuery();
SqlConnection MyCon1 = new SqlConnection("server=hemalatha\\sqlexpress;integr ated security=sspi;database=persons");
MyCon1.Open();
SqlCommand Mycmd1 = new SqlCommand("insert into persons values 'scope',123,123,12,'scope'");
Mycmd1.ExecuteNonQuery();
}
Finished Transaction Scope
Use the following namespace
using System.Transactions;
By default the namespace will not be available Go to add reference and add the namespace
protected void Button2_Click(object sender, EventArgs e)
{
//Transaction Scope
//Here we use two different connections demo and demo1
//No need to use commit or rollback
//Will take implicitly only scope.complete and scope.dispose with try catch is necessary
using (TransactionScope scope = new TransactionScope())
{
int check = 0;
try
{
using (SqlConnection MyCon = new SqlConnection("server=hemalatha\\sqlexpress;integr ated security=sspi;database=demo"))
{
MyCon.Open();
SqlCommand Mycmd = new SqlCommand("insert into t1 values(2,'oooo','10/10/2007',3,3,3)", MyCon);
check=Mycmd.ExecuteNonQuery();
using (SqlConnection MyCon1 = new SqlConnection("server=hemalatha\\sqlexpress;integr ated security=sspi;database=demo1"))
{
try
{
MyCon1.Open();
check = 0;
SqlCommand Mycmd1 = new SqlCommand("insert into demotable1 values ('3','rrr')", MyCon1);
check= Mycmd1.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write("Error in 2nd");
Response.Write(ex.Message);
}
}
}
}
catch (Exception ex)
{
Response.Write("error in 1st");
Response.Write(ex.Message);
}
if (check > 0)
{
Response.Write("Transaction Committed");
scope.Complete();
}
else
{
Response.Write("Transaction RolledBack");
scope.Dispose();
}
}
}