不錯,很好,以後防注入批量事務提交雖然麻煩點研究了幾個小時,但不會是問題了
1 SqlCommand cmd; 2 HelpSqlServer helps = new HelpSqlServer(); 3 //定義SqlParameter數組 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 try 35 { 36 cmd.ExecuteNonQuery(); 37 transaction.Commit(); 38 } 39 catch 40 { 41 transaction.Rollback(); 42 } 43 } 44 45 } View Code