RollBack全部执行查询

时间:2017-08-14 21:26:34

标签: c# mysql commit rollback

如何在C#

中执行的MYSQL查询中回滚所有已执行查询
if (cn0.State != System.Data.ConnectionState.Open) cn0.Open();
            cm0.Connection = cn0;
            cm0.CommandText = "SET AUTOCOMMIT=0 ";
            cm0.ExecuteNonQuery();
            cm0.CommandTimeout = 0;//unlimited

            tr = cn0.BeginTransaction(System.Data.IsolationLevel.Serializable);
            cm0.Transaction = tr;

            try
            {
                cm0.CommandText = "CREATE TABLE IF NOT EXISTS i2019.Produkh LIKE i2019.karyawanh ";
                cm0.ExecuteNonQuery();

                cm0.CommandText = "CREATE TABLE IF NOT EXISTS i2019.Profileh LIKE i2019.karyawanh ";
                cm0.ExecuteNonQuery();

                cm0.CommandText = "CREATE TABLE IF NOT EXISTS i2019.Customerh LIKE i2019.karyawanh ";
                cm0.ExecuteNonQuery();

                cm0.CommandText = "CREATE TABLE IF NOT EXISTS i2019.supplierh LIKE i2019.karyawanh ";
                cm0.ExecuteNonQuery();

                cm0.CommandText = "INSERT INTO i2019.produkh (idx,idH,idb,Faktur,tglB,jmlB,ket,edit,bf,modifat) " +
                    "SELECT idx, idH, idb, Faktur, tglB, jmlB, ket, edit, bf, modifat FROM karyawanh.t1 WHERE jmlB < 0";
                                cm0.ExecuteNonQuery();

                tr.Commit();
                if (cn0.State != System.Data.ConnectionState.Closed) cn0.Close();
                tr.Dispose();
            }
            catch
            {
                tr.Rollback();
                if (cn0.State != System.Data.ConnectionState.Closed) cn0.Close();
                tr.Dispose();
                throw;
            }

但是在CREATE SUPPLIERH TABLE上执行查询时出现错误 所以回滚并关闭连接。

我希望 所有表:

  • Produkh
  • profileh
  • customerh
  • supplierh

未创建,因为其他

上存在错误

但实际上是在回滚之后 仅回滚 supplierh 表 而另一个没有回滚。

请告诉我怎么做?

0 个答案:

没有答案