Commit or rollback the transaction based on the outcome.?
Example:
Follow:
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();
try
SqlCommand command1 = new SqlCommand("UPDATE Customers SET
Balance = Balance - 100", connection, transaction);
SqlCommand command2 = new SqlCommand("UPDATE Accounts SET
Balance = Balance + 100", connection, transaction);
command1.ExecuteNonQuery();
command2.ExecuteNonQuery();
transaction.Commit(); // Commit the transaction
catch (Exception)
transaction.Rollback(); // Rollback if there is an error
finally
connection.Close();