Transaction application-Execute multiple SQL statements

Source: Internet
Author: User

The transaction is atomic, either not executed, or fully executed, once a permanent save is successfully executed . and these are due to the atomicity of the transaction and the persistence of the database. . The following is a batch operation that unifies data modifications in a database , Leverage to Transactions .

  TODO: Bulk modifying data in a database code:
#region Extensionmethod///<summary>///Chen Fanglin-March 22, 2014 15:17:59//</summar, according to the number of students assigned y>//<param name= "Strstudentnoscore" > SQL statement for Add-on </param>//<returns> return results of execution </retu Rns> public bool Teagivescores (string[] strstudentnoscore) {//Open database connection Sqlcon.            Open ();            Declares the SQL transaction sqltransaction Mytrans;              SQL Command object SqlCommand mycmd = new SqlCommand (); Declares the connection database myCMD of the Command object.            Connection = Sqlcon; Turn on transaction Mytrans = Sqlcon.            BeginTransaction (); Sets the transaction mycmd that will be executed in the Command object.            Transaction = Mytrans;                try {//Traversal SQL statement in string array for (int i = 0; i < strstudentnoscore.length; i++)                                       {mycmd.commandtext = Strstudentnoscore[i]; int R =mycmd.                ExecuteNonQuery ();          }      Transaction submission Mytrans.commit ();            return true;                    } catch (Exception) {//Execution error, transaction rollback if (Mytrans! = null)                Mytrans.rollback ();            return false;                    } finally {//close connection if (Sqlcon! = null) Sqlcon.            Close (); }} #endregion Extensionmethod

Summary

The transaction operation is approximate (temperature)

BeginTrans: Start transaction
CommitTrans: Commit Transaction
RollbackTrans: Rolling back a transaction

It is primarily a good package for data processing, as a unit of processing. Work with multiple rows of SQL statements in conjunction with several common SQL objects.

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.