今天由于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();//关闭数据库连接
}