当我使用正确的用户名和密码登录我的应用程序时,我收到错误的用户名或密码的错误消息,然后下次我点击登录按钮登录工作正常。我是一个delphi业余爱好者所以不要难以评判我。
type
TfmLogin = class(TForm)
pnlMain: TPanel;
lblUserName: TLabel;
SQLConnection: TSQLConnection;
DataSource: TDataSource;
ClientDataSet: TClientDataSet;
SQLQuery: TSQLQuery;
DataSetProvider: TDataSetProvider;
edtUserName: TEdit;
lblPassword: TLabel;
edtPassword: TEdit;
btnRegistration: TButton;
btnLogin: TButton;
SQLTable: TSQLTable;
procedure btnRegistrationClick(Sender: TObject);
procedure btnLoginClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fmLogin: TfmLogin;
implementation
这是匹配功能
function MatchPass(Table : TSQLTable; const UserName, Password : string): Boolean;
begin
Result := Table.Locate('Username;Password', VarArrayOf([username,password]),[]);
end;
这是登录按钮程序
procedure TfmLogin.btnLoginClick(Sender: TObject);
var
UserName : string;
Password : string;
begin
UserName := edtUserName.Text;
Password := edtPassword.Text;
if(MatchPass(SQLTable, UserName, Password)) then //1.Why first time returns false
begin
with TfmMain.Create(nil) do
try
ShowModal; //3.And opens my main form
finally
free;
end;
end
else
ShowMessage('Wrong name or pass'); //2.And enters else and the second time returns true
end;
答案 0 :(得分:-2)
为什么不(从头):
procedure TLogin_Form.Button1Click(Sender: TObject);
begin
Query1.Active:=false;
Query1.SQL.Clear;
Query1.SQL.Text:= 'select [user],[password] from users where [user] ='+QuotedStr(cxlookupcombobox1.text) +' and [password]='+QuotedStr(cxTextEdit1.Text);
Query1.Open;
if Query1.FieldByName('password').AsString<>''
then begin
ModalResult := mrOK ;
end else
ModalResult := mrNone;
你不需要匹配任何东西,因为密码必须匹配。