using MySql.Data.MySqlClient; // Declare a connection and command MySqlConnection connection = new MySqlConnection("connectionString"); MySqlCommand cmd = new MySqlCommand("query", connection); // Open the connection connection.Open(); // Execute the query and create a data reader MySqlDataReader reader = cmd.ExecuteReader(); // Use the data reader // Close the data reader reader.Close(); // Close the connection connection.Close();
using MySql.Data.MySqlClient; // Declare a connection and command and data reader using (MySqlConnection connection = new MySqlConnection("connectionString")) { using (MySqlCommand cmd = new MySqlCommand("query", connection)) { // Open the connection connection.Open(); // Execute the query and create a data reader using (MySqlDataReader reader = cmd.ExecuteReader()) { // Use the data reader } } }In this example, we use the using statement to automate the closing of the MySqlDataReader object. By using this approach, we don't have to call the Close method explicitly. When the using block is exited, the object is automatically disposed of and any resources associated with it are released. Package library: MySql.Data(MySql.Data.MySqlClient) libarary.