using MySql.Data.MySqlClient; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); MySqlTransaction transaction = connection.BeginTransaction(); try { // Execute database operations here transaction.Commit(); } catch { transaction.Rollback(); } }
using MySql.Data.MySqlClient; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); MySqlTransaction _transaction1 = connection.BeginTransaction(); try { // Execute database operations here MySqlTransaction _transaction2 = connection.BeginTransaction(); // Execute more database operations here _transaction2.Commit(); _transaction1.Commit(); } catch { _transaction2.Rollback(); _transaction1.Rollback(); } }In this example, nested transactions are used. The outer transaction is rolled back if any of the inner transactions fail. Package library: MySql.Data.