When you do a project, you need to implement the database's transaction. I feel that I use the SQLTransaction to write code. It is too much trouble, I want to summarize a generic approach to put on the data layer for multiplexing. I wrote a simple method. Method can be satisfied Demand. :)
Method of implementing two SQL statements at the same time:
public static void ExecuteSqlTran (string SQLString1, string SQLString2) {using (SqlConnection conn = new SqlConnection (strDBConnectionString)) {conn.Open (); SqlCommand cmd = new SqlCommand (); cmd.Connection = conn; SqlTransaction tx = conn.BeginTransaction (); Cmd.Transaction = TX; try {cmd.commandtext = sqlstring1; cmd.executenonQuery (); cmd.commandtext = SQLString2; cmd.executenonquery (); tx.commit ();} catch (system.data.sqlclient. SQLException E) {tx.rollback (); throw new exception (E.MESSAGE);}}}
If there are multiple SQL statements that need to be implemented in a transaction, use the following method:
///
Although it is simple, it really saves a lot of time :)