mysql 事务处理
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>mysql数据库
/// <param name="SQLStringList">多条SQL语句</param>
public static void ExecuteSqlTran(List<string> SQLStringList)
{
using (MySqlConnection conn = new MySqlConnection(MySqlHelper.ConnStr))
{
conn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
MySqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
//后来加上的
if (n > 0 && (n % 500 == 0 || n == SQLStringList.Count - 1))
{
tx.Commit();
tx = conn.BeginTransaction();
}
}
//tx.Commit();//原来一次性提交
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}