Friday, May 15, 2009

Distributed Transaction(DTC) using .Net

Add the below reference to the disired Project
System.Transactions

(Using is key word in C# it'll release to garbage collection after usage)


private void button1_Click(object sender, EventArgs e)
{
using (TransactionScope objScope = new TransactionScope())
{
string conString1 = "Data Source=sfa_dev;Persist Security Info=True;User ID=ez;Password=ez;Unicode=True";
string conString2 = "Data Source=CHANDIMA\\SQLEXPRESS;Initial Catalog=Exam;Integrated Security=True";
using (OracleConnection Con1 = new OracleConnection(conString1))
{
OracleCommand ocmd = new OracleCommand();
ocmd.CommandText ="Insert into dtctest values (1,'Test Adddress')";
Con1.Open();
ocmd.ExecuteNonQuery();
Con1.Close();
}
using(SqlConnection con2=new SqlConnection(conString2))
{
SqlCommand scmd=new SqlCommand();
scmd.CommandText ="Insert into tbl_Student values (3,'chandima W','Test Add1',1,'~/Test')";
con2.Open();
scmd.ExecuteNonQuery();
con2.Close();
}
objScope.Complete();
}
}

No comments:

Post a Comment