in my program i need to check if a record in the database already exists in the table using the if statement.
using c# i am trying to do this through an sql con
If someday you want to use EF just do:
private MyDb db = new MyDb();
public bool UserExists(string userName, string password){
return db.Users.Any(x => x.user_name.Equals(userName, StringComparison.InvariantCultureIgnoreCase)
&& x.password.Equals(password, StringComparison.InvariantCultureIgnoreCase));
}
Or do a generic method, so you can handle multiple entites:
public bool EntityExists(Expression> predicate) where T : class, new()
{
return db.Set().Any(predicate);
}
Usage:
EntityExists(x => x.user_name.Equals(userName, StringComparison.InvariantCultureIgnoreCase)
&& x.password.Equals(password, StringComparison.InvariantCultureIgnoreCase));