if you are using entity framework you can use it.
using (var dataContext = new SchoolMSDbContext())
{
using (var trans = dataContext.Database.BeginTransaction(IsolationLevel.ReadCommitted))
{
try
{
// your query
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
Console.WriteLine(ex.InnerException);
}
}
}
Or you can try this
using (var dataContext = new SchoolMSDbContext())
{
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try
{
//your query
transaction.Complete();
}
catch (Exception ex)
{
transaction.Dispose();
Console.WriteLine(ex.InnerException);
}
}
}
for this you'll have to this references.
System.Transactions
for more information check this links
https://msdn.microsoft.com/en-us/data/dn456843.aspx
https://msdn.microsoft.com/en-us/library/2k2hy99x(v=vs.110).aspx