3

I would like to create a type of login window in Delphi. Unfortunately I can't get it to match the username and password.

I have a basic .mdb database, with an Users table. In this table, there is a Username and Password. I want Delphi to check the username and password in the database and if it matches those in the edit boxes, it goes to the next form or shows a message, otherwise it does nothing. When I enter the database's first row, username and password values, I get success, but with the second, nothing. I feel like I need a way to get it to move on to the second rows values and check those and so on. There is also currently no relationships in the database.

This is my layout of Data Access: ADOConnection -> ADOTable -> DataSource

Thanks in advance!

4
  • Can you include the structure of your table and the code which you are using to validate the user and password? Commented Jul 13, 2012 at 23:46
  • you should better never store a plain password in the db, but use a hash with some salt (e.g. the user name)... Commented Jul 14, 2012 at 7:54
  • I get the idea that the OP might think that your Hash with Some Salt was actually some breakfast food, involving potatoes. Commented Jul 16, 2012 at 20:57
  • Yes, using a hash function with salt is the way to go. You can use md5 or sha-1 function. It is great for password and credit card number, for example. Commented Jul 17, 2012 at 4:04

1 Answer 1

2

As per your guess, one solution can be to move record by record to check each one. Example:

function MatchPass(Table: TADOTable; const Name, Pass: string): Boolean;
begin
  Result := False;
  Table.First;
  while not Table.Eof do begin
    if Table.FieldByName('Username').AsString = Name then begin
      Result := Table.FieldByName('Password').AsString = Pass;
      Exit;
    end;
    Table.Next;
  end;
end;

Can be called like:

  if MatchPass(ADOTable1, Edit1.Text, Edit2.Text) then
    ..


Another solution can be to let the ADOTable search for a corresponding record:

function MatchPass(Table: TADOTable; const Name, Pass: string): Boolean;
begin
  Result := Table.Locate('Username;Password', VarArrayOf([Name, Pass]), []);
end;
Sign up to request clarification or add additional context in comments.

4 Comments

Is not better just use a sql sentence?
@RRUZ - I'm not sure one or the other would have a significant advantage. But we already have a ADOTable as given in the question.
Thanks Sertac! There is no reason why I should use ADOTable, instead of ADOQuery. So out of curiosity, what would the SQL statement look like?
@coder - Probably something like "select password from passwords where user = :Username", then supply a value for username parameter and read the password field..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.