I am aware of the solution to implement SQL Server transactions in .net C# with the "using" keyword and with code like this:
InsertDetails()
{
using (TransactionScope ts = new TransactionScope())
{
InsertName();//SQL functions to insert name into name table
Insertaddress();//SQL functions to insert address into address table
InsertPhoneNo();//SQL functions to insert phone number into contact table
ts.Complete();
}
}
But say for example I wished to instead pass the sql server transaction as a parameter to many different functions for different database queries, without having the using statement example.
After calling all the functions in the code path I would then like to make a call to commit the data and if something went wrong then perform a rollback.
Pseudo code would look like this
InsertDetails()
{
var transaction = new Transaction();
var sqlcon = new SqlConnection();
InsertName(transaction, sqlcon);//SQL functions to insert name into name table
Insertaddress(transaction, sqlcon);//SQL functions to insert address into address table
InsertPhoneNo(transaction, sqlcon);//code to insert phone no into contact table
try
{
ts.commit();
}
catch(Exception ex)
{
ts.rollback();
}
}