Simple Login Form C# With Database MySQL

Introduction 

 
In this article, we will create a simple login form in C#. We will use the database MySQL for this tutorial.
 
Let’s following this tutorial in below to simple login code in c# windows application.
 
Step 1
 
Create a database in MySQL with the name “test” and create a table with the name “user”, like shown below.

Simple Login Form C# With Database Mysql
 
Step 2
 
Create a new application project. In Visual Studio, on the menu click File> New > Project. For more details, see the following menu on the display.

Simple Login Form C# With Database Mysql
 
Step 3
 
Then the window New Project will appear.

Simple Login Form C# With Database Mysql
 
Step 4
 
Write down the name of the project that will be created on a field Name. Specify the directory storage project by accessing the field Location. Next, give the name of the solution in the Solution Name. Then click OK.

Simple Login Form C# With Database Mysql
 
Step 5
 
Create a new windows form, like below

Simple Login Form C# With Database Mysql
 
Step 6
 
Create a new class for connection database and write the following program listing:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using MySql.Data.MySqlClient;  
  6. using System.Windows.Forms;  
  7. using System.Data;namespace Connection_DB  
  8. {  
  9.     class connection  
  10.     {  
  11.           
  12.         MySql.Data.MySqlClient.MySqlConnection conn;  
  13.         string myConnectionString;  
  14.         static string host = "localhost";  
  15.         static string database = "test";  
  16.         static string userDB = "ecco";  
  17.         static string password = "password";  
  18.         public static string strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;  
  19.         public bool Open()  
  20.         {  
  21.             try  
  22.             {  
  23.                 strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;  
  24.                 conn = new MySqlConnection(strProvider);  
  25.                 conn.Open();  
  26.                 return true;  
  27.             }  
  28.             catch (Exception er)  
  29.             {  
  30.                 MessageBox.Show("Connection Error ! " + er.Message, "Information");  
  31.             }  
  32.             return false;  
  33.         }        public void Close()  
  34.         {  
  35.             conn.Close();  
  36.             conn.Dispose();  
  37.         }        public DataSet ExecuteDataSet(string sql)  
  38.         {  
  39.             try  
  40.             {  
  41.                 DataSet ds = new DataSet();  
  42.                 MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);  
  43.                 da.Fill(ds, "result");  
  44.                 return ds;  
  45.             }  
  46.             catch (Exception ex)  
  47.             {  
  48.                 MessageBox.Show(ex.Message);  
  49.             }  
  50.             return null;  
  51.         }        public MySqlDataReader ExecuteReader(string sql)  
  52.         {  
  53.             try  
  54.             {  
  55.                 MySqlDataReader reader;  
  56.                 MySqlCommand cmd = new MySqlCommand(sql, conn);  
  57.                 reader = cmd.ExecuteReader();  
  58.                 return reader;  
  59.             }  
  60.             catch (Exception ex)  
  61.             {  
  62.                 MessageBox.Show(ex.Message);  
  63.             }  
  64.             return null;  
  65.         }        public int ExecuteNonQuery(string sql)  
  66.         {  
  67.             try  
  68.             {  
  69.                 int affected;  
  70.                 MySqlTransaction mytransaction = conn.BeginTransaction();  
  71.                 MySqlCommand cmd = conn.CreateCommand();  
  72.                 cmd.CommandText = sql;  
  73.                 affected = cmd.ExecuteNonQuery();  
  74.                 mytransaction.Commit();  
  75.                 return affected;  
  76.             }  
  77.             catch (Exception ex)  
  78.             {  
  79.                 MessageBox.Show(ex.Message);  
  80.             }  
  81.             return -1;  
  82.         }  
  83.     }  

Step 7
 
Next step, go back to the Windows form and view code to write the following program listing
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using MySql.Data.MySqlClient;namespace Connection_DB  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         connection con = new connection();  
  14.         string id, username, password, firstname, lastname, address;  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }        private void btnLogin_Click(object sender, EventArgs e)  
  19.         {  
  20.             try  
  21.             {                if (txtUsername.Text != "" && txtPassword.Text != "")  
  22.                 {  
  23.                       
  24.                     con.Open();  
  25.                     string query = "select id,username,password,firstname,lastname,address from user WHERE username ='" + txtUsername.Text + "' AND password ='" + txtPassword.Text + "'";  
  26.                     MySqlDataReader row;  
  27.                     row = con.ExecuteReader(query);  
  28.                     if (row.HasRows)  
  29.                     {  
  30.                         while (row.Read())  
  31.                         {  
  32.                             id = row["id"].ToString();  
  33.                             username = row["username"].ToString();  
  34.                             password = row["password"].ToString();  
  35.                             firstname = row["firstname"].ToString();  
  36.                             lastname = row["lastname"].ToString();  
  37.                             address = row["address"].ToString();  
  38.                         }                        MessageBox.Show("Data found your name is " + firstname + " " + lastname + " " + " and your address at " + address);  
  39.                     }  
  40.                     else  
  41.                     {  
  42.                         MessageBox.Show("Data not found""Information");  
  43.                     }  
  44.                 }  
  45.                 else  
  46.                 {  
  47.                     MessageBox.Show("Username or Password is empty""Information");  
  48.                 }  
  49.             }  
  50.             catch  
  51.             {  
  52.                 MessageBox.Show("Connection Error""Information");  
  53.             }            
  54.         }  
  55.     }  

Step 8
 
After you write down the program listings, press the F5 key to run the program and if you successfully connect your database, the result is:

Simple Login Form C# With Database Mysql
 
We have explained how to make a simple login form in C# for beginners, for those of you who want to download the source code of the program, you also can. Hopefully, this discussion is helpful to you.
 
You can see Simple Login Form C# With Database Mysql from the Github project here.
 
Thank you for reading this article about Simple Login Form C# With Database Mysql. I hope this article was useful for you. Visit My Github about .Net Csharp here.