delphi中实现用户名和密码登录界面,需要和SQL数据库中的用户名和密码匹配才能登录

SQL数据库中的表名为user,有2列,分别为username(主键)和password,delphi中的登录界面需要连接user表,界面如下:
当输入用户名和密码与user表中的某一行对应的时候才能登录,其他均有相应的报错。不知道怎么弄,希望高手给予指导,最好有详细步骤,谢谢!

unit Unitdenglu;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, ExtCtrls, jpeg;

type
TForm6 = class(TForm)
Edit2: TEdit;
Edit1: TEdit;
Button2: TButton;
Button1: TButton;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
Image1: TImage;
procedure Button1Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form6: TForm6;
err_cou:integer;//定义变量
yhname,pass : string;
s:string;
stringlength : byte;
implementation

uses Unit1;//引用主窗体

{$R *.dfm}

procedure TForm6.Button1Click(Sender: TObject);//点击登录按钮
begin
if edit2.text='' then
begin
Application.MessageBox('密码不能为空,请重新输入','系统提示',mb_IconInformation+mb_OK);
Edit2.SetFocus;
Exit;
end;
ADOquery1.sql.clear;
ADOquery1.sql.add('select 密码 from 用户密码表 where 用户名='+''''+trim(edit1.text)+'''');
if ADOquery1.Active then
ADOquery1.close;
ADOquery1.open;
if trim(edit2.text)<>trim(ADOquery1.fieldbyname('密码').asstring)
then
begin
if err_cou=2 then
begin
application.messagebox('三次登录的用户名或密码错误',' 系统退出',mb_IconInformation+mb_OK);
form1.close;
end;
Application.MessageBox('密码或工号不正确','请重新输入!',mb_IconInformation+mb_OK);
Edit2.SetFocus;
err_cou:=err_cou+1;
exit;
end;
close;

end;

procedure TForm6.Edit1Change(Sender: TObject);//用户框输入
begin
if edit1.GetTextLen<3 then
begin
Button1.Enabled := false;
end;
if edit1.GetTextLen=3 then
begin
Button1.Enabled := true;
end;
end;

procedure TForm6.Button2Click(Sender: TObject);//点击退出按钮
begin
formdenglu.close;
form1.Close;
end;

procedure TForm6.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (edit1.text<>'') AND (key=#13) then
edit2.setfocus;
end;

procedure TForm6.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
button1.Enabled:=true;
if key=#13 then begin
Button1Click(Sender);
end;
end;

end.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-16
ADOQUery1.SQL:=
'SELECT * FROM user where username=''用户名'' AND password=''password'''
adoquery1.execute;
if ADOQuery.RecordCount>0 then
允许登陆
第2个回答  推荐于2017-12-16
procedure TForm1.btnLoginClick(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from user where username=:username and password=:password');
ADOQuery1.Parameters.ParamByName('username').Value := edtUserName.Text;
ADOQuery1.Parameters.ParamByName('password').Value := edtPassword.Text;
try
ADOQuery1.Open;
if ADOQuery1.RecordCount > 0 then
begin
ShowMessage('登录成功!');
Close;//关闭登录窗体
end
else
ShowMessage('用户名或密码错误!');
except
ShowMessage('数据连接失败!');
end;
end;

如果你要详细提示错误则用下边的
procedure TForm1.btnLoginClick(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from user where username=:username');
ADOQuery1.Parameters.ParamByName('username').Value := edtUserName.Text;
try
ADOQuery1.Open;
if ADOQuery1.RecordCount > 0 then
begin
ADOQuery1.First;
if ADOQuery1.FieldByName('username').AsString = edtPassword.Text then
begin
ShowMessage('登录成功!');
Close;//关闭登录窗体
end
else
ShowMessage('密码错误');
end
else
ShowMessage('用户名错误!');
except
ShowMessage('数据连接失败!');
end;
end;本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网