SqlParameter transaction batch data insertion, sqlparameter batch

Source: Internet
Author: User

SqlParameter transaction batch data insertion, sqlparameter batch

Good, very good. In the future, anti-injection batch transaction commit will not be a problem even though it takes several hours to study it.

 

1 SqlCommand cmd; 2 HelpSqlServer helps = new HelpSqlServer (); 3 // define the SqlParameter Array 4 SqlParameter [] param = new SqlParameter [4]; 5 6 7 protected void Page_Load (object sender, EventArgs e) 8 {9 var strsql = ""; 10 for (int I = 0; I <4; I ++) 11 {12 strsql + = "insert into Questions (PID) values"; 13 strsql + = "(@ PID" + I + ");"; 14 15 param [I] = new SqlParameter ("@ PID" + I + "", SqlDbType. uniqueIdentifier); 16 param [I]. value = Guid. newGuid (); 17 18} 19 20 using (SqlConnection conn = new SqlConnection (HelpSqlServer. connectionString) 21 {22 cmd = new SqlCommand (strsql, conn); 23 24 25 conn. open (); 26 SqlTransaction transaction = conn. beginTransaction (); 27 28 cmd. transaction = transaction; 29 foreach (SqlParameter para in param) 30 {31 cmd. parameters. add (para); 32} 33 34 try35 {36 cmd. executeNonQuery (); 37 transaction. commit (); 38} 39 catch40 {41 transaction. rollback (); 42} 43} 44 45}View Code

 

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.