Right now i have code that initiates transactions on SQL Server using the intended method:
ExecuteNonQuery(connection, "BEGIN TRANSACTION");
try
{
DoABunchOnStuff(connection);
DoSomeMoreStuff(connection);
JustAFewMoreThings(connection);
ExecuteNonQuery(connection, "COMMIT TRANSACTION");
}
catch (Exception)
{
ExecuteNonQuery(connection, "ROLLBACK TRANSACTION");
throw;
}
Now i’m looking at thinking about the possibility of investigating the idea of using the transaction abstraction provided by ADO.NET:
DbTransaction trans = connection.BeginTransaction();
try
{
DoABunchOnStuff(connection);
DoSomeMoreStuff(connection);
JustAFewMoreThings(connection);
trans.Commit();
}
catch (Exception)
{
trans.Rollback();
throw;
}
Problem with this simple conversion from SQL Server based transactions, to ADO.NET transactions, is the error:
ExecuteNonQuery requires the command
to have a transaction when the
connection assigned to the command is
in a pending local transaction. The
Transaction property of the command
has not been initialized.
Am i correct in assuming that if i wanted to use ADO.NET transactions i would have to completely gut the infrastructure, passing along a DbTransaction object to every method that does, or may, operate inside a transaction?
Yes, exactly – you basically need to associate the transaction you’ve created with each SqlCommand that ought to be executed under that transaction’s umbrella – so you’d have to have something like:
and inside those methods something along the lines of: