I have been debugging for half a day because l transaction is not effective today. It is because the MySQL database MyISAM type table does not support transactions.
Later, I changed the table type to InnoDB, and the transaction processing took effect. It was depressing for half a day !!!
Basic transaction code:
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, "13:06:44", "4ffff4444444", 4666 );
Mysqlconnection con = new mysqlconnection (mysqlconnstring. metsconnection );
Con. open (); // open the database connection before calling the Method
Mysqltransaction TRAN = con. begintransaction (); // You must enable the database connection before generating the sqltransaction object.
Try
{
Mysqlcommand comm = new mysqlcommand (); // create and declare a command object
Comm. Connection = con;
Comm. Transaction = Tran; // set the property to sqltransaction object.
Comm. commandtext = "delete from mets_daily_drinking_info"; // set
Comm. executenonquery (); // execute an SQL statement
Comm. commandtext = sqlstr; // set the SQL statement to be executed by the sqlcommand object
Comm. executenonquery (); // execute an SQL statement
Tran. Commit ();
}
Catch
{
Tran. rollback ();
Throw;
}
Finally
{
Con. Close (); // close the database connection
}