MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand command = new MySqlCommand("SELECT COUNT(*) FROM table_name", connection); connection.Open(); int count = (int)command.ExecuteScalar(); connection.Close();
MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand command = new MySqlCommand("SELECT MAX(column_name) FROM table_name", connection); connection.Open(); int maxValue = (int)command.ExecuteScalar(); connection.Close();This code connects to the database using the connectionString, executes the SQL query to find the maximum value in the column, retrieves the value as an object, and casts it to an integer. Package/Library: To use MySqlCommand and MySqlConnection in C#, you need to add the MySql.Data.EntityFramework NuGet package to your project.