0% found this document useful (0 votes)
43 views

How To Get Data From Database and Check If User Put Correct Username and Password To Log In?

The document provides three examples of C# code to query a SQL database and check a username and password for user login. The first example selects all records from the Users table without parameters. The second example selects from Users where the username and password match the parameters passed in. It checks if a login is successful based on the results. The third example uses a SQL command to select the ID from the Users table where the username and password match the parameters, and returns a boolean if the result is not null.

Uploaded by

Noman Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

How To Get Data From Database and Check If User Put Correct Username and Password To Log In?

The document provides three examples of C# code to query a SQL database and check a username and password for user login. The first example selects all records from the Users table without parameters. The second example selects from Users where the username and password match the parameters passed in. It checks if a login is successful based on the results. The third example uses a SQL command to select the ID from the Users table where the username and password match the parameters, and returns a boolean if the result is not null.

Uploaded by

Noman Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

using(SqlConnection con = new SqlConnection(@"Data

Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated
Security=True"))
{
SqlCommand cmd = new SqlCommand("select * from Users;");
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
con.Close();
}
How to get data from database and check if user put correct username and password to log
in ?

using(SqlConnection con = new SqlConnection(@"Data


Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated
Security=True"))
{
SqlCommand cmd = new SqlCommand("select * from Users where username like
@username and password = @password;");
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
cmd.Connection = con;
con.Open();

DataSet ds = new DataSet();


SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();

bool loginSuccessful = ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0));

if (loginSuccessful)
{
Console.WriteLine("Success!");
} else {
Console.WriteLine("Invalid username or password");
}
}

string Command = "SELECT Id FROM User WHERE Username = @Username AND Password =
@Password;";
using (SqlConnection myConnection = new SqlConnection(ConnectionString))
{
myConnection.Open();
using (SqlCommand myCommand = new SqlCommand(Command, myConnection))
{
myCommand.Parameters.Add("@Username", tbUser.Text);
myCommand.Parameters.Add("@Password", tbPass.Text);
return myCommand.ExecuteScalar() != null;
}
}

You might also like