Transaction application-run multiple SQL statements

Source: Internet
Author: User

The transaction is atomic, either not running, or fully running, once it is successfully run and permanently saved . and this is precisely because of the atomicity of the transaction and the persistence of the database. . The following is a batch operation that unifies data churn in a database , Leverage to Transactions .

  TODO: Batch churn 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> Returns the result of the run </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 run 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) {//Run 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 a 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 lines of SQL statements in conjunction with several objects that SQL uses frequently.

Transaction application-run multiple SQL statements

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.