using MySql.Data.MySqlClient; using System; // Create a new MySqlCommand object MySqlCommand cmd = new MySqlCommand("SELECT * FROM users"); try { // Execute the query MySqlDataReader reader = cmd.ExecuteReader(); // Read the data while (reader.Read()) { Console.WriteLine(reader["username"]); } // Dispose of the reader reader.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { // Dispose of the command object cmd.Dispose(); }
using MySql.Data.MySqlClient; using System; // Create a new MySqlCommand object MySqlCommand cmd = new MySqlCommand("DELETE FROM users WHERE id = @id"); // Add a parameter cmd.Parameters.AddWithValue("@id", 1); try { // Execute the query int rowsAffected = cmd.ExecuteNonQuery(); Console.WriteLine("{0} rows deleted.", rowsAffected); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { // Dispose of the command object cmd.Dispose(); }This example creates a MySqlCommand object to execute a DELETE query and adds a parameter to the command object. After executing the query, the MySqlCommand object is disposed in the finally block. The MySqlCommand.Dispose() method is part of the MySql.Data.MySqlClient package library.