private bool ExecuteTransaction(List<string> list) { using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["LocalConnectionString"].ToString())) { SqlCommand command = new SqlCommand(); SqlTransaction transaction = null; try { connection.Open(); transaction = connection.BeginTransaction(); command.Connection = connection; command.Transaction = transaction; for (int i = 0; i < list.Count; i++) { command.CommandText = list[i]; command.ExecuteNonQuery(); } transaction.Commit(); connection.Close(); return true; } catch { transaction.Rollback(); connection.Close(); return false; } } }
list是要执行的SQL语句。
这个人写的→ http://www.cnblogs.com/lengzhan/
时间: 2024-12-22 14:48:19