using MySql.Data.MySqlClient; MySqlConnection connection = new MySqlConnection("Server=localhost;Database=mydatabase;Uid=myusername;Pwd=mypassword;"); connection.Open(); MySqlCommand command = new MySqlCommand("SELECT * FROM mytable", connection); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1)); } reader.Close(); connection.Close();
using MySql.Data.MySqlClient; MySqlConnection connection = new MySqlConnection("Server=localhost;Database=mydatabase;Uid=myusername;Pwd=mypassword;"); connection.Open(); MySqlCommand command = new MySqlCommand("SELECT * FROM mytable WHERE id=@id", connection); command.Parameters.Add("@id", MySqlDbType.Int32).Value = 1; MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1)); } reader.Close(); connection.Close();In this example, we create a MySqlCommand object with a parameterized SQL statement that includes a parameter for the id value. We then add a parameter to the command object and set its value to 1. We use the Read() method to loop through the result set and print out the values of the first two columns in each row that matches the id parameter. Package Library: MySql.Data