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