using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; namespace mysql事务测试 { class Program { static void Main(string[] args) { try { using (MySqlConnection conn = new MySqlConnection("Database=xini2ng;Data Source=127.0.0.1;User Id=root;Password=root;pooling=false;CharSet=utf8")) { conn.Open(); MySqlTransaction transaction = conn.BeginTransaction(); MySqlCommand cmd = conn.CreateCommand(); cmd.Transaction = transaction; try { cmd.CommandText = "INSERT INTO tbl_phonenumber VALUES(‘1‘,‘1‘,‘2‘,‘3‘)"; int x = cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO tbl_phonenumber VALUES(‘1‘,‘1‘,‘2‘,‘4‘,‘5‘)"; int y = cmd.ExecuteNonQuery(); transaction.Commit(); } catch (Exception) { try { transaction.Rollback(); } catch (Exception) { throw; } } } } catch (Exception) { throw; } } } }
话不多说,就这样:
时间: 2024-10-17 13:38:32