In Ado.net we can implement transaction for Single Database and also multiple database ( known as distributed database )
This is how we can implement transaction into single database using Ado.net object
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
void transactionTest1()
{
using (var transaction = new TransactionScope())
{
//data manipulation done here
transaction.Complete();
}
This is how we can implement transaction into single database using Ado.net object
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
void transactionTest1()
{
string strConnString = "myconnectionstring";
SqlTransaction objTrans = null;
using (SqlConnection objConn = new SqlConnection(strConnString))
{
objConn.Open();
objTrans = objConn.BeginTransaction();
SqlTransaction objTrans = null;
using (SqlConnection objConn = new SqlConnection(strConnString))
{
objConn.Open();
objTrans = objConn.BeginTransaction();
SqlCommand objCmd1 = new SqlCommand("insert into tableOne values(100)", objConn);
SqlCommand objCmd2 = new SqlCommand("insert into tableTwo values(200)", objConn);
SqlCommand objCmd2 = new SqlCommand("insert into tableTwo values(200)", objConn);
try {
objCmd1.ExecuteNonQuery();
objCmd2.ExecuteNonQuery();
objCmd1.ExecuteNonQuery();
objCmd2.ExecuteNonQuery();
objTrans.Commit();
}
catch (Exception)
{
objTrans.Rollback();
}
finally {
objConn.Close();
}
}
}
catch (Exception)
{
objTrans.Rollback();
}
finally {
objConn.Close();
}
}
}
Another way to use transaction can be
{
//data manipulation done here
transaction.Complete();
}
No comments:
Post a Comment