我正在使用 mysql connector6.2.3.0(.net) 从 C# 连接 mysql 5.1。我想一次插入 3 个表。所以,我为此使用事务。假设在将数据插入到我的第三张表,则事务不回滚。数据被插入到前两张表中。
这是我的代码...
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();
}
我想,最初,我想禁用自动提交......但我不知道我必须在哪里设置这个......
请告诉我这是怎么回事...