using MySql.Data.MySqlClient; MySqlConnection conn = new MySqlConnection("server=localhost;database=mydb;uid=root;password=mypassword;"); conn.Open(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM customers"; MySqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(rdr["FirstName"].ToString() + " " + rdr["LastName"].ToString()); } rdr.Close(); conn.Close();
using MySql.Data.MySqlClient; MySqlConnection conn = new MySqlConnection("server=localhost;database=mydb;uid=root;password=mypassword;"); conn.Open(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "INSERT INTO customers (FirstName, LastName) VALUES (@firstName, @lastName)"; cmd.Parameters.AddWithValue("@firstName", "John"); cmd.Parameters.AddWithValue("@lastName", "Doe"); int rowsAffected = cmd.ExecuteNonQuery(); conn.Close();This code establishes a connection to a MySQL database, creates a MySqlCommand object to execute an insert query against the customers table with parameterized values for first name and last name, and then executes the command using the ExecuteNonQuery method. The method returns the number of rows affected by the query.