今天由於l事務不生效問題調試了半天,居然是Mysql資料庫MyISAM類型表不支援事務
後面把表類型更改成InnoDB,交易處理才生效,鬱悶了半天啊!!!
基礎事務代碼:
string sqlStr = @"Insert Into `mets_daily_drinking_info`( PatientID,CureID,DrinkingTime,DrinkingAmount,DrinkingAmountUnit)
Values({0},{1},'{2}',{3},{4})";
sqlStr = string.Format(sqlStr, 5555, 1, "2009-4-4 13:06:44", "4ffff4444444", 4666);
MySqlConnection con = new MySqlConnection(MySqlConnString.METSConnection);
con.Open();//在調用方法前開啟資料庫連接
MySqlTransaction tran = con.BeginTransaction();//在產生SqlTransaction對象前必須開啟資料庫連接
try
{
MySqlCommand comm = new MySqlCommand();//建立和聲明Command對象
comm.Connection = con;
comm.Transaction = tran;//設定屬性為SqlTransaction對象
comm.CommandText = "Delete From mets_daily_drinking_info";//設定
comm.ExecuteNonQuery();//執行SQL語句
comm.CommandText = sqlStr; //設定SqlCommand對象要執行的SQL語句
comm.ExecuteNonQuery();//執行SQL語句
tran.Commit();
}
catch
{
tran.Rollback();
throw;
}
finally
{
con.Close();//關閉資料庫連接
}