Simple Login Form C# With Database Mysql

Ecco Suprastyo
4 min readJun 19, 2019

--

You can read see FULL SECTION this article in this http://camellabs.com/simple-login-form-csharp-with-database-mysql/ or Visit my site at camellabs.com.

In this article, we can to try create Simple Login Form C# With Database Mysql. We will use database Mysql for this tutorial.

Let’s following this tutorial in below to simple login code in c# windows application.

  1. Create database in mysql with name “test” and create table with name “user”, like the below.

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.

3. Then will appear the window New Project like the look below.

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.

5. Create a new windows form like the below

6. Create a new class for connection database and write the following program listing :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using System.Data;
namespace Connection_DB
{
class connection
{

MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
static string host = "localhost";
static string database = "test";
static string userDB = "ecco";
static string password = "password";
public static string strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;
public bool Open()
{
try
{
strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;
conn = new MySqlConnection(strProvider);
conn.Open();
return true;
}
catch (Exception er)
{
MessageBox.Show("Connection Error ! " + er.Message, "Information");
}
return false;
}
public void Close()
{
conn.Close();
conn.Dispose();
}
public DataSet ExecuteDataSet(string sql)
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
da.Fill(ds, "result");
return ds;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
public MySqlDataReader ExecuteReader(string sql)
{
try
{
MySqlDataReader reader;
MySqlCommand cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
return reader;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
public int ExecuteNonQuery(string sql)
{
try
{
int affected;
MySqlTransaction mytransaction = conn.BeginTransaction();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
affected = cmd.ExecuteNonQuery();
mytransaction.Commit();
return affected;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return -1;
}
}
}

7. Next step, Back to windows form and view code to write the following program listing:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Connection_DB
{
public partial class Form1 : Form
{
connection con = new connection();
string id, username, password, firstname, lastname, address;
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
if (txtUsername.Text != "" && txtPassword.Text != "")
{

con.Open();
string query = "select id,username,password,firstname,lastname,address from user WHERE username ='" + txtUsername.Text + "' AND password ='" + txtPassword.Text + "'";
MySqlDataReader row;
row = con.ExecuteReader(query);
if (row.HasRows)
{
while (row.Read())
{
id = row["id"].ToString();
username = row["username"].ToString();
password = row["password"].ToString();
firstname = row["firstname"].ToString();
lastname = row["lastname"].ToString();
address = row["address"].ToString();
}
MessageBox.Show("Data found your name is " + firstname + " " + lastname + " " + " and your address at " + address);
}
else
{
MessageBox.Show("Data not found", "Information");
}
}
else
{
MessageBox.Show("Username or Password is empty", "Information");
}
}
catch
{
MessageBox.Show("Connection Error", "Information");
}
}
}
}

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

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 also can.Hopefully this discussion is helpful to you

You can see Simple Login Form C# With Database Mysql from Github project in Here.

Thank you for reading this article about Simple Login Form C# With Database Mysql, I hope this article is useful for you. Visit My Github about .Net Csharp in Here

--

--