这次很快就找到了入口
在MySQL官方文档界面查找(MySQL Server—>MySQL Reference Manual—>选定自己的版本,当然也可以下载的 )
不过说真的还是下载下来HTML比较方便点,打开Index.html就是目录了,我在其中搜索字段‘transaction’然后可以快速定位自己关心的内容。
我已经得到的信息
InnoDB
实现,有一些属性,这些属性被统称为:(按首字母缩写)ACID具体包括: To disable autocommit mode implicitly for a single series of statements, use the START TRANSACTION statement:
START TRANSACTION
命令可以禁用autocommit。START TRANSACTION;
//……
COMMIT;
With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state.
我的参考:C#事务处理(Execute Transaction)实例解析
踩了一个坑(错误一):C#入门 错误笔记
//别忘了导包
using MySql.Data.MySqlClient;
//……
//参考逻辑
string insertSql = "INSERT INTO teacher (teacher.id, teacher.utc8_create, teacher.utc8_modify, teacher.`name`, teacher.office, teacher.other, teacher.contact) VALUES (@ID,@Create,@Modify,@Name,@Office,@Other,@Contact)";
string Conn = "Database='for_aurora';Data Source='localhost';User Id='root';Password='root';charset='utf8'";
MySqlConnection mySqlConnection = new MySqlConnection(Conn);//创建连接
mySqlConnection.Open();//先打开连接
MySqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction();//后开启事务
try
{
//执行命令>1
MySqlCommand mSqlCommand_1 = new MySqlCommand();
mSqlCommand_1.Connection = mySqlConnection;
mSqlCommand_1.CommandText = insertSql;
mSqlCommand_1.Parameters.Add(new MySqlParameter("@ID", Guid.NewGuid().ToString("N")));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Create", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Modify", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Name", "name"));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Office", "Office"));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Other", "Other"));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Contact", "Contact"));
mSqlCommand_1.ExecuteNonQuery();
//执行命令>1
//MySqlCommand mSqlCommand_2 = new MySqlCommand();
mSqlCommand_1.Parameters.Clear();
mSqlCommand_1.Connection = mySqlConnection;
mSqlCommand_1.CommandText = insertSql;
mSqlCommand_1.Parameters.Add(new MySqlParameter("@ID", Guid.NewGuid().ToString("N")));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Create", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Modify", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Name", "name"));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Office", "Office"));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Other", "Other"));
mSqlCommand_1.Parameters.Add(new MySqlParameter("@Contact", "Contact"));
mSqlCommand_1.ExecuteNonQuery();
//命令若干……
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
mySqlTransaction.Rollback();
mySqlConnection.Close();
}
finally {
Console.WriteLine("状态:" + mySqlConnection.State);
if (mySqlConnection.State != ConnectionState.Closed) {
mySqlTransaction.Commit();
mySqlConnection.Close();
}
}