Find Max Number Series in Database Using C#

Find Max Number Series in Database Using C#

In this article I will show you how to find max number series in database using c#, let’s follow code below

Step 1:

Create Project And Add Reference To C# WinForms Project. (visit the link to the first tutorial on how to add reference to our C# project)

How to connect MySQL Database to C# Tutorial and Source code

Step 2:

Open MySQL Workbench, right click and create schema (new database), give database name as “sampledb” and create table in database and give a name as “information”, then create columns id, emp_id, name, designation, username and password.

Find Max Number Series in Database Using C# - Step 2
Find Max Number Series in Database Using C# – Step 2

Step 3:

Back to windows form application and design form like this (see below image)

Find Max Number Series in Database Using C# - Form Design
Find Max Number Series in Database Using C# – Form Design

Step 4:

Code for button(Find Max Number) – The Max Number in Database is 5

using MySql.Data.MySqlClient;
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;

namespace findmaxnumberindatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection("datasource= localhost; database=sampledb;port=3306; username = root; password= db1234");
            con.Open();
            MySqlCommand cmd = new MySqlCommand("select MAX(emp_id) as emp_id from information", con);
            MySqlDataReader myread = cmd.ExecuteReader();
            if (myread.Read())
            {
                textBox1.Text = myread.GetInt32("emp_id").ToString();
                myread.Close();
                cmd.Dispose();
            }
            cmd.Dispose();
            myread.Close();
            con.Close();
        }
    }
}

Code Explanation:

This code explain, how to find max number series in database, and display to textbox

Output:

Find Max Number Series in Database Using C# - Output
Find Max Number Series in Database Using C# – Output

Find Max Number Series in Database Using C# Free Download Source code

Mark Jaylo

[email protected]

https://www.youtube.com/c/MarkTheProgrammer

You may visit our facebook page for more information, inquiries and comments.

Hire our team to do the project.

Comments

comments

, ,

Post navigation