using MySql.Data.MySqlClient; var connectionString = "server=localhost;user id=root;password=root;database=mydatabase"; var query = "SELECT price FROM products WHERE name='Shirt'"; using var connection = new MySqlConnection(connectionString); using var command = new MySqlCommand(query, connection); connection.Open(); using MySqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { float price = reader.GetFloat(0); Console.WriteLine($"The price is {price:F2}"); }
using MySql.Data.MySqlClient; var connectionString = "server=localhost;user id=root;password=root;database=mydatabase"; var query = "SELECT AVG(score) FROM scores WHERE game='Tetris'"; using var connection = new MySqlConnection(connectionString); using var command = new MySqlCommand(query, connection); connection.Open(); using MySqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { float averageScore = reader.GetFloat(0); Console.WriteLine($"The average score is {averageScore:F0}"); }In this example, we connect to a MySQL database, execute a query to retrieve the average score of a game, and retrieve the value as a `float`. We then display the average score as a whole number.