How to Create a Login Form in C#.NET using SQL Server Database and Visual Studio 2022? [With Source Code]
SqlConnection con = new SqlConnection("Data Source=YOUR-COMPUTER-NAME\\SQLEXPRESS;Initial Catalog=loginapp;Integrated Security=True;TrustServerCertificate=True");
con.Open();
string query = "SELECT COUNT(*) FROM loginapp WHERE username=@username AND password=@password";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@username", txtUser.Text);
cmd.Parameters.AddWithValue("@password", txtPass.Text);
int count = (int)cmd.ExecuteScalar();
con.Close();
if (count > 0)
{
MessageBox.Show("login success", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("error in login");
}
//password show hide:
txtPassword.UseSystemPasswordChar = !checkBox1.Checked;
//close form:
this.Close();
FULL TUTORIAL: