How to display MaxID, Sum, and Count all Records in sql database C# windows application.

Опубликовано: 17 Март 2026
на канале: Safi Tech
1,054
8

All the source code is here!

///////Mainform(InfoForm) code//////
private void Info_Load(object sender, EventArgs e)
{
Database.DBGeneral obj = new Database.DBGeneral();
obj.display_lable("select max(ID) as general from TablePractice", lbmaxid);
obj.display_lable("select sum(ID) as general from TablePractice", lbsum);
obj.display_lable("select count(ID) as general from TablePractice", lbtotal);

}
/////////////////////////////


//////DBGeneral class code/ Function////////

using MetroFramework.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BCSBatch9E.Database
{
class DBGeneral:DBConnection
{
public SqlCommand CMD;

//Display MaxID on Label...
public void display_lable(string query, Label lb)
{
OpenConnection();
CMD.CommandText = query;
SqlDataReader rd = CMD.ExecuteReader();
if (rd.Read())
{
lb.Text = rd["general"].ToString();
}
CloseConnection();
}

///////////////////////////////////////////////////////
}
}