I have this:
using (var con= new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString))
{
try
{
// many transactions
}
catch (Exception e)
{
con.BeginTransaction().Rollback();
}
}
Will this work is my question.. I know another method is to make a transaction then open it then rollback.
You could use a TransactionScope variable in a using block at the same level of the using block of the SqlConnection
using (TransactionScope scope = new TransactionScope())
using (var con= new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString))
{
try
{
// many transactions
scope.Complete();
}
catch (Exception e)
{
// Not needed any rollback, if you don't call Complete
// a rollback is automatic exiting from the using block
// con.BeginTransaction().Rollback();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With