C# 使用MySQL事务的使用方法

 

//使用事务来处理多条数据,如果不成功则回滚

public void getCheckListSubmit()
{
string _conStr = "................";//连接字符串
OdbcConnection _odbcCon = new OdbcConnection(_conStr);
_odbcCon.Open();

OdbcTransaction trans = _odbcCon.BeginTransaction(IsolationLevel.ReadCommitted);
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = trans.Connection;
cmd.Transaction = trans;

string sqlStr = "";
try
{
sqlStr = ".................";

cmd.CommandText = sqlStr;
cmd.ExecuteNonQuery();

sqlStr2 = ".................";
cmd.CommandText = sqlStr2;
cmd.ExecuteNonQuery();

}
catch
{
trans.Rollback();
hidsysMsg.Value = "Insert Failed";
_odbcCon.Close();
return;
}
trans.Commit();
hidsysMsg.Value = "Insert Succeeded";
_odbcCon.Close();

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。