I am doing a password authentication query like this.
if exists(select UserID from users where UserName=@UserName and Password=@Password)
select UserID from users where UserName=@UserName and Password=@Password
else
select 0
but it seems to me that I am querying twice for a single result
Can I just do this
Select UserID Where Username = @UserName and Password = @Password
and when I get my results just check the reader for number of results?
if (!myReader.HasRows)
MessageBox.Show("UserName not found or Password invalid");
else
//do login stuff
Guess what I am asking is, which of these is better, or are they the same?
Use this:
Your solution with checking number of records would work too of course.
Note that it will always return exactly one record and will fail if
userNameis not unique and there are duplicates of@userName.As noted by @Andriy M,
COALESCEwill not help here since it’s being internally rewritten into aCASEprone to the same problem.Answering your question: yes, the original batch would access
userstwice.