In C#, I have three statements in a method that insert data into a database. The first statement inserts and I get an ID from it. This ID is used in the next two statements.
I have two cases to handle:
1.) The second statement can return false if certain input conditions aren’t met. In that case, I’d like to rollback the first statement.
2.) If either of the statements generate an error, they should all rollback.
I thought to wrap all three statements in a transaction scope but it will only rollback on exceptions, which only covers the second case. What is the best way handle both cases?
I’m using DataContext via a DBML with LINQ 2 SQL
With TransactionScope() you need to call Complete() method to commit the changes.
Othewise all pending changes will be rolled back, so your (1) is also covered