using MySql.Data.MySqlClient; // Create a new transaction MySqlTransaction transaction = conn.BeginTransaction(); try { // Insert a new customer record MySqlCommand cmd1 = new MySqlCommand( "INSERT INTO customers (name) VALUES (@name);", conn, transaction); cmd1.Parameters.AddWithValue("@name", "John Doe"); cmd1.ExecuteNonQuery(); // Get the ID of the inserted customer record int customerId = (int)cmd1.LastInsertedId; // Insert a new order record referring to the customer MySqlCommand cmd2 = new MySqlCommand( "INSERT INTO orders (customer_id, amount) VALUES (@customerId, @amount);", conn, transaction); cmd2.Parameters.AddWithValue("@customerId", customerId); cmd2.Parameters.AddWithValue("@amount", 100.0); cmd2.ExecuteNonQuery(); // Commit the transaction transaction.Commit(); } catch (Exception ex) { // Something went wrong, roll back the transaction transaction.Rollback(); Console.WriteLine("Error: " + ex.Message); }In this example, a new customer record is inserted, and then an order record is inserted that refers to the customer by their ID. If either insert command fails, the transaction is rolled back - meaning neither record is inserted. If both commands succeed, the transaction is committed and the changes are made permanent. Overall, the MySql.Data.MySqlClient library provides a powerful and flexible set of tools for working with MySQL databases in C#. Transactions are an essential feature for managing database changes in a controlled and consistent way.