So I have this sql from c# to insert purchase info into a table (sql server 2005), and purchase items (multiple) into another table.
I would like the 2 insert statements to be run in a transaction by calling BeginTransaction.
I notice that I can do it either from the SqlConnection object or SqlCommand object. My guts tells me that I should do it through the connection object, as I will be using 1 command object for each insert, but they both share the same connection.
Am I right?
In general is there a difference between the two?
Create a SqlTransaction from the connection (BeginTransaction), then pass this in to the each SqlCommand object. There’s a constructor that takes the SqlTransaction as parameter or just set the SqlCommand.Transaction property.
Something like: (sorry formatting difficult on iPad)