Create Login Form Using C# with MySQL Database
In this tutorial, I will show you how to create simple login form using c# . Let’s follow this tutorial 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 “login”, then create columns id, username, and password.
Step 3:
Back to the windows forms application and design login forms like this (see below picture)
- txtusername
- txtpassword
- btnsignin
- btnclose
Steps 4:
Source Code for Sign In button(btnsignin) clicked event:
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_FULL_COURSE_WITH_MYSQL_DATABASE { public partial class loginform : Form { public loginform() { InitializeComponent(); } private void btnsignin_Click(object sender, EventArgs e) { MySqlConnection con = new MySqlConnection("datasource= localhost; database=sampledb;port=3306; username = root; password= db1234"); //open connection con.Open(); MySqlCommand cmd = new MySqlCommand("select * from login where username = '" + txtusername.Text + "' AND password = '" + txtpassword.Text + "'", con); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { MessageBox.Show("Successfully Sign In!", "VINSMOKE MJ", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Username And Password Not Match!", "VINSMOKE MJ", MessageBoxButtons.OK, MessageBoxIcon.Error); } txtusername.Text = string.Empty; txtpassword.Text = string.Empty; reader.Close(); cmd.Dispose(); con.Close(); // always close connection } } } }
Code Explanation:
These code will connect to the MySQL Database and read input username and password , if the username and password is match in login table or Database it will trigger as successful login else username and password not match.
Result:
Output
Create Login Form Using C# with MySQL Database Free Download Source code
Mark Jaylo
C# Full Course With MySql Database Part 2(Create LogIn Form). – YouTube
You may visit our facebook page for more information, inquiries and comments.
Hire our team to do the project.