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

↘锁芯ラ 提交于 2019-12-23 05:29:16

问题


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...


回答1:


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

ALTER TABLE tablename ENGINE = innodb

refer Documentation



来源:https://stackoverflow.com/questions/14994995/my-transaction-is-not-rollback-when-something-goes-wrong-in-c-sharp-with-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!