当东西在C#中出现问题与MySQL我的交易没有回滚? [英] my transaction is not rollback when something goes wrong in C# with mysql?

查看:348
本文介绍了当东西在C#中出现问题与MySQL我的交易没有回滚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被用mysql connector6.2.3.0(.NET)。我想在time.So插入3个表,我使用的交易,that.Suppose,遇到了一些错误,当MySQL 5.1中从C#连接,同时插入数据进入我的第三个表,那么交易不rollback.The数据插入到前两个表。

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.

这是我的代码...

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

指的文档

这篇关于当东西在C#中出现问题与MySQL我的交易没有回滚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆