using MySql.Data.MySqlClient; string connectionString = "server=localhost;user=root;database=mydatabase;password=mypassword;"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); string query = "INSERT INTO `mytable` (name, age) VALUES ('John', 30)"; MySqlCommand command = new MySqlCommand(query, connection); command.ExecuteNonQuery(); connection.Close();
using MySql.Data.MySqlClient; string connectionString = "server=localhost;user=root;database=mydatabase;password=mypassword;"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); string query = "SELECT * FROM `mytable`"; MySqlCommand command = new MySqlCommand(query, connection); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { string name = reader["name"].ToString(); int age = Convert.ToInt32(reader["age"]); Console.WriteLine("Name: {0}, Age: {1}", name, age); } connection.Close();This example demonstrates how to use MySqlCommand to retrieve data from a MySQL table. A connection is established to the database, and then a SELECT statement is executed using the MySqlCommand object. The results are then read using a MySqlDataReader object. Package Library: MySqlCommand is part of the MySQL Connector/NET library, which is a fully-managed ADO.NET data provider for MySQL. This library is developed and maintained by Oracle and is available for download on the MySQL website.