C # enable SQL statements for multi-strip parameters of transactions,
/// <Summary> /// enable the SQL statement for multi-strip parameters of transaction commit // </summary> /// <param name = "mainSql"> Primary table SQL </param> /// <param name = "mainParam"> parameters of the master table </param> /// <param name = "detailSql"> statement of a detail table </param >/// <param name = "detailParam"> parameters corresponding to the detail table </param> /// <returns> whether the transaction is successful </returns> public static bool UpdateByTran (string mainSql, sqlParameter [] mainParam, string detailSql, List <SqlParameter []> detailParam) {SqlConnection Conn = new SqlConnection (connString); SqlCommand cmd = new SqlCommand (); cmd. connection = conn; try {conn. open (); cmd. transaction = conn. beginTransaction (); // enable the transaction if (mainSql! = Null & mainSql. Length! = 0) {cmd. commandText = mainSql; cmd. parameters. addRange (mainParam); cmd. executeNonQuery ();} foreach (SqlParameter [] param in detailParam) {cmd. commandText = detailSql; cmd. parameters. clear (); cmd. parameters. addRange (param); cmd. executeNonQuery ();} cmd. transaction. commit (); // submit the transaction return true;} catch (Exception ex) {if (cmd. transaction! = Null) {cmd. transaction. rollback (); // Rollback transaction} // write exception information to the log string errorInfo = "Call UpdateByTran (string mainSql, SqlParameter [] mainParam, string detailSql, list <SqlParameter []> detailParam) Method
Error occurred. Details: "+ ex. Message; WriteLog (errorInfo); throw ex;} finally {if (cmd. Transaction! = Null) {cmd. Transaction = null; // clear the Transaction} conn. Close ();}}
Reprinted from CSDN