简体   繁体   中英

my transaction is not rollback when something goes wrong in C# with mysql?

I am connecting mysql 5.1 from C# by using mysql connector6.2.3.0(.net).I want to insert 3 tables at a time.So, i am using transaction for that.Suppose, when some errors encountered while inserting data into my 3rd table, then the transaction is not rollback.The data is inserted into first two tables.

This is my code...

MySqlDataAdapter da = new MySqlDataAdapter();
 DBUtil cUtil = new DBUtil();
 MySqlConnection mysqlCon=null;
 MySqlTransaction txn = null;

try
{
    mysqlCon = cUtil.getDbConnection();
    txn = mysqlCon.BeginTransaction();

    //1 
    sql = "insert into test_details()";
    da.InsertCommand = new MySqlCommand(sql, mysqlCon,txn);
    da.InsertCommand.ExecuteNonQuery();

    //2
    sql = "insert into task_details()";
    da.InsertCommand = new MySqlCommand(sql, mysqlCon,txn);
    da.InsertCommand.ExecuteNonQuery();

    sql = "select task_id from task_details where test_id='" + testId + "'";
    da.SelectCommand = new MySqlCommand(sql, mysqlCon,txn);
    dt1 = new System.Data.DataTable();
    da.Fill(dt1);
    string task_id = dt1.Rows[0]["task_id"].ToString();

    //3
    sql = "insert into test_evaluators()";
    da.InsertCommand = new MySqlCommand(sql, mysqlCon,txn);
    da.InsertCommand.ExecuteNonQuery();

    txn.Commit();
    mysqlCon.Close();
    da.Dispose();
}
catch (Exception e)
{
    txn.Rollback();
    mysqlCon.Close();
}

i think, initially, i want to disable autocommit... but i dont know where i have to set this...

please suggest me what's going wrong with this...

As we found out that it is DB engine problem, you can use

ALTER TABLE tablename ENGINE = innodb

refer Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM