Calculating the Percentage of Male and Female Students from a MySQL Database Using C#
In this C# tutorial, we will learn how to calculate and display the percentage values from a MySQL database table in C# using Visual Studio editor.
We will create two classes:
1- "MY_DB": This class will handle the connection with the MySQL database.
2- "MY_CALC": This class will be responsible for counting the values we need.
Additionally, we will have a form named "calculate_percentage" to display the percentage values.
//************** End calculate_percentage Form **************//
We will create two classes:
1- "MY_DB": This class will handle the connection with the MySQL database.
2- "MY_CALC": This class will be responsible for counting the values we need.
Additionally, we will have a form named "calculate_percentage" to display the percentage values.
WATCH THIS C# TUTORIAL
Project Source Code:
//************** Start MY_DB Class **************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;
namespace csharp__calculate_percentage
{
class My_DB
{
// the connection
MySqlConnection con = new MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=student_db");
// get the connection
public MySqlConnection getConnection
{
get
{
return con;
}
}
// open the connection
public void openConnection()
{
if ((con.State == ConnectionState.Closed))
{
con.Open();
}
}
// close the connection
public void closeConnection()
{
if ((con.State == ConnectionState.Open))
{
con.Close();
}
}
}
}
//************** End MY_DB Class **************//
//************** Start MY_CALC Class **************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace csharp__calculate_percentage
{
class My_CALC
{
My_DB db = new My_DB();
// function to execute count query
string execCount(string query)
{
MySqlCommand command = new MySqlCommand(query, db.getConnection);
db.openConnection();
string count = command.ExecuteScalar().ToString();
db.closeConnection();
return count;
}
// function to return the total students count in the database
public string totalStudents()
{
return execCount("SELECT COUNT(*) FROM `student`");
}
// function to return the total MALE students count in the database
public string totalMaleStudents()
{
return execCount("SELECT COUNT(*) FROM `student` WHERE gender = 'MALE'");
}
// function to return the total FEMALE students count in the database
public string totalFemaleStudents()
{
return execCount("SELECT COUNT(*) FROM `student` WHERE gender = 'FEMALE'");
}
}
}
//************** End MY_CALC Class **************//
//************** Start calculate_percentage Form **************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace csharp__calculate_percentage
{
class My_CALC
{
My_DB db = new My_DB();
// function to execute count query
string execCount(string query)
{
MySqlCommand command = new MySqlCommand(query, db.getConnection);
db.openConnection();
string count = command.ExecuteScalar().ToString();
db.closeConnection();
return count;
}
// function to return the total students count in the database
public string totalStudents()
{
return execCount("SELECT COUNT(*) FROM `student`");
}
// function to return the total MALE students count in the database
public string totalMaleStudents()
{
return execCount("SELECT COUNT(*) FROM `student` WHERE gender = 'MALE'");
}
// function to return the total FEMALE students count in the database
public string totalFemaleStudents()
{
return execCount("SELECT COUNT(*) FROM `student` WHERE gender = 'FEMALE'");
}
}
}
//************** End calculate_percentage Form **************//
OUTPUT:
Download Projects Source Code