You can insert multiple records into the database within a single transaction to improve performance and ensure data consistency. Use the below code.
using Dapper;
using Devart.Data.MySql;
using System.Data;
namespace DapperMySqlExample
{
class Program
{
static void Main(string[] args)
{
using (IDbConnection db = new MySqlConnection(DatabaseConnection.ConnectionString))
{
try
{
db.Open();
Console.WriteLine("Connection to MySQL successful!");
InsertCustomers(db);
DisplayInsertedCustomers(db);
}
catch (Exception ex)
{
Console.WriteLine($"Connection failed: {ex.Message}");
}
}
}
private static void InsertCustomers(IDbConnection db)
{
Console.WriteLine("\nInserting 10 new customers...");
var newCustomers = new List<Customers> {
new Customers {
First_Name = "Ethan", Last_Name = "Harris", Email = "[email protected]", Phone = "123-111-7890", Address = "321 Main St"
},
new Customers {
First_Name = "Sophia", Last_Name = "Clark", Email = "[email protected]", Phone = "234-222-7890", Address = "432 Elm St"
},
new Customers {
First_Name = "Mason", Last_Name = "Lewis", Email = "[email protected]", Phone = "345-333-7890", Address = "543 Oak St"
},
new Customers {
First_Name = "Olivia", Last_Name = "Walker", Email = "[email protected]", Phone = "456-444-7890", Address = "654 Maple St"
},
new Customers {
First_Name = "Liam", Last_Name = "Hall", Email = "[email protected]", Phone = "567-555-7890", Address = "765 Pine St"
},
new Customers {
First_Name = "Isabella", Last_Name = "Allen", Email = "[email protected]", Phone = "678-666-7890", Address = "876 Birch St"
},
new Customers {
First_Name = "Noah", Last_Name = "Young", Email = "[email protected]", Phone = "789-777-7890", Address = "987 Cedar St"
},
new Customers {
First_Name = "Ava", Last_Name = "King", Email = "[email protected]", Phone = "890-888-7890", Address = "198 Walnut St"
},
new Customers {
First_Name = "Lucas", Last_Name = "Wright", Email = "[email protected]", Phone = "901-999-7890", Address = "209 Chestnut St"
},
new Customers {
First_Name = "Mia", Last_Name = "Scott", Email = "[email protected]", Phone = "012-000-7890", Address = "310 Spruce St"
}
};
string insertQuery =
@"
INSERT INTO Customers(first_name, last_name, email, phone, address)
VALUES(:First_Name,:Last_Name,:Email,:Phone,:Address);
"
;
using (var transaction = db.BeginTransaction())
{
try
{
db.Execute(insertQuery, newCustomers, transaction);
transaction.Commit();
Console.WriteLine("Batch insert successful!");
}
catch (Exception ex)
{
transaction.Rollback();
Console.WriteLine($"Insert failed: {ex.Message}");
}
}
}
private static void DisplayInsertedCustomers(IDbConnection db)
{
Console.WriteLine("\nFetching last 10 inserted customers...");
string selectQuery = "SELECT customer_id AS Customer_Id, first_name AS First_Name, last_name AS Last_Name, email, phone, address FROM Customers ORDER BY customer_id DESC LIMIT 10;";
var customers = db.Query<Customers>
(selectQuery);
foreach (var customer in customers)
{
Console.WriteLine($"{customer.Customer_Id}: {customer.First_Name} {customer.Last_Name} - {customer.Email}, {customer.Phone}, {customer.Address}");
}
}
}
}
The results are displayed within the application.