C# And MySQL Execute COUNT, MAX MIN, AVG

How To Execute MySQL Database Count Max  Min Avg In C#

c# and database count minimum maximum average

In This C# Tutorial  We Will See How To Use MySQL ExecuteScalar Function To:
 - execute count command to get database table's records count.
 - execute min command to get database minimum value.
 - execute max command to get database maximum value.
 - execute avg command to get database average value.
In Csharp Programming Language And Visual Studio Editor.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Csharp_Tutorials
{
    public partial class MySQL_ExecuteScalar : Form
    {
        public MySQL_ExecuteScalar()
        {
            InitializeComponent();
        }

        // mysql connection
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='mydb';username=root;password=");
        
        // button count
        private void buttonCount_Click(object sender, EventArgs e)
        {

            MySqlCommand count_cmd = new MySqlCommand("SELECT COUNT(*) FROM dgv_data",connection);

            connection.Open();

            textBox1.Text = count_cmd.ExecuteScalar().ToString();

            connection.Close();

        }

        // button max
        private void buttonMax_Click(object sender, EventArgs e)
        {

            MySqlCommand max_cmd = new MySqlCommand("SELECT MAX(age) FROM dgv_data", connection);

            connection.Open();

            textBox1.Text = max_cmd.ExecuteScalar().ToString();

            connection.Close();

        }

        // button min
        private void buttonMin_Click(object sender, EventArgs e)
        {

            MySqlCommand min_cmd = new MySqlCommand("SELECT MIN(*) FROM dgv_data", connection);

            connection.Open();

            textBox1.Text = min_cmd.ExecuteScalar().ToString();

            connection.Close();

        }

        // button avg
        private void buttonAvg_Click(object sender, EventArgs e)
        {

            MySqlCommand avg_cmd = new MySqlCommand("SELECT AVG(age) FROM dgv_data", connection);

            connection.Open();

            textBox1.Text = avg_cmd.ExecuteScalar().ToString();

            connection.Close();

        }
    }
}

      
///////////////OUTPUT:

C# Use ExecuteScalar With MySQL Database