Introduction
In this article, we will create a simple login form in C#. We will use the database MySQL for this Article
Let’s follow this tutorial 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”, as shown below.
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.
Step 3. Then, the window New Project will appear.
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConcoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 5. Create a new Windows form like below.
Step 6. Create a new class for the 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;
}
}
}
Step 7. Next step, go back to the Windows form and view the 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 is at " + address);
}
else
{
MessageBox.Show("Data not found", "Information");
}
}
else
{
MessageBox.Show("Username or Password is empty", "Information");
}
}
catch
{
MessageBox.Show("Connection Error", "Information");
}
}
}
}
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.
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,