Reputation: 17196
I want to do an insert and an update on 2 separate tables, but have them be in 1 transaction).
Essentially in pseudocode I want to do something like:
MySqlTransaction trans = null;
try
{
_Connection.Open();
trans = _Connection.BeginTransaction();
insertCmd.Transaction = trans;
updateCmd.Transaction = trans;
Int32 id = insertCmd.ExecuteNonQuery();
updateCmd.Parameters.Add(new MySqlParameter("oid", MySqlDbType.Int32).Value = id);
updateCmd.ExecuteNonQuery();
}
catch(MySqlException)
{
if(trans != null)
trans.RollBack();
}
finally
{
_Connection.Close();
}
is this possible or am I going about this the wrong way?
Upvotes: 6
Views: 1833
Reputation: 29897
Yes you can if:
Upvotes: 6
Reputation: 13853
Yes, you can execute multiple commands within a single transaction. This would let you do exactly what you are trying to do: all commands, or none of them.
Upvotes: 3