LINQ - Login Database

113 Views Asked by At

I want to login with existing userID and password in database.

When I use SQL statement in C#, I use this code

string query = "SELECT * FROM MsUser WHERE Username='"+textBoxUsername.Text+"' and Password='"+textBoxPass.Text+"'";
DataTable dt = con.AdapterQuery(query);
if (dt.Rows.Count>0)
{
    MessageBox.Show("Success loging");
}
else
    MessageBox.Show("Failed");

But i want to use LINQ, what is the code when using LINQ statement??

1

There are 1 best solutions below

3
On

You simply read user data for given UserName and Password. In LINQ you can replace this with:

var user = Context.Users.SingleOrDefault(u => u.Username == "userName" && u.Password == "password");

if(user != null)
{
   MessageBox.Show("Success loging");
}
else
{
    MessageBox.Show("Failed");
}