C # enable SQL statements for multi-strip parameters of transactions,

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.