我以前没有用过deophi,向解决如下的问题
有一个数据库equip里面有一张表user;
现在做一个登陆的程序,登陆时连接数据库,登陆名称和登陆密码输入与数据库user表的数据相比较,对了就进入查询,不对就抱错,请问登陆时怎么做?
给出源码,
很急,很急,马上就要用,解决了给100分
有一个数据库equip里面有一张表user;
现在做一个登陆的程序,登陆时连接数据库,登陆名称和登陆密码输入与数据库user表的数据相比较,对了就进入查询,不对就抱错,请问登陆时怎么做?
给出源码,
很急,很急,马上就要用,解决了给100分
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
ADOTable1: TADOTable;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not adotable1.Locate('name',edit1.Text,[]) then
showmessage('不存在这个用户,登陆失败')
else
if adotable1.FieldValues['password']=edit2.Text then
begin
form2.Show;
form1.Hide;
end
else
begin
showmessage('密码错误,请从新登陆');
application.Terminate;
end;end;procedure TForm1.Button2Click(Sender: TObject);
begin
form1.close;
end;end.
unit Unit5;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBTables;type
TForm5 = class(TForm)
Button1: TButton;
GroupBox1: TGroupBox;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Table1: TTable;
procedure Button1Click(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form5: TForm5;
sUserID,sUserPW: string;implementationuses Unit1;{$R *.dfm}procedure TForm5.Button1Click(Sender: TObject);
begin
try
table1.Filter:='name='''+edit1.text+'''';
table1.Filtered:=true;
table1.Open;
Except
Application.MessageBox('数据库连接失败,请于系统管理员联系。','数据库连接错误!',MB_OK);
Application.Terminate;
end;
if table1.recordcount=1 then
begin
if table1['password']=edit2.text then
begin
sUserID := Trim(edit1.Text);
sUserPW := Trim(edit2.Text);
self.ModalResult:=mrOK;
end
else
begin
Application.MessageBox('密码输入有误,请确认密码!','密码不正确!',MB_OK);
edit2.SetFocus;
end;
end
else Application.MessageBox('请确认用户名是否正确!','无此用户!',MB_OK); edit1.SetFocus;
end;procedure TForm5.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then { 如 果 按 下 了 回 车 键 }
begin
Key := #0; { 吃 掉 回 车 键 }
Perform(WM_NEXTDLGCTL,0,0); { 移 动 到 下 一 个 控 制 }
end;
end;procedure TForm5.Button2Click(Sender: TObject);
begin
close;
end;end.
// edit2.text为密码输入框
user.Filter:='id='''+edit1.Text+'''';
user.Filtered:=true;
user.Open;
if user.RecordCount=1 then //判断登陆名
begin
if user['passwd']=edit2.Text then
begin
....查询
end
else showmessage('密码不对!');
end
else showmessage('无此用户!')
procedure Tlogin.LoginBtnClick(Sender: TObject);
begin
username:=login.Edit1.Text;
paswd:=login.Edit2.Text;
pathst:=getcurrentdir()+'\mdb\equip.mdb;';
st:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\EquipManage\mdb\equip.mdb;Persist Security Info=False';
ADOConnection1.ConnectionString:=st;
ADOConnection1.Connected:=true;
if not ADOTable1.Locate('username',username,[])then
begin
FLogError.ShowModal;
ADOTable1.Close;
ADOConnection1.Close;
end
else if ADOTable1.FieldValues['pswd']=paswd then
begin
FsearchG1.ShowModal;
login.Hide;
ADOTable1.Close;
ADOConnection1.Close;
end
else begin
FLogError.ShowModal;
ADOTable1.Close;
ADOConnection1.Close;
end;
end;end.
说是什么对想打开是操作不被允许
>> adotable1.Connection:=ADOConnection1;
>> adotable1.TableName:=user;
if not ADOTable1.Locate('username',username,[])then
begin
DataModule1.UserTable.Active:=true;
if EdtUser.Text='' then
begin
ShowMessage('请输入用户名!');
EdtUser.SetFocus;
exit;
end;
if DataModule1.UserTable.Locate('username;userpwd',VarArrayOf([EdtUser.Text,EdtPassword.Text]),
[]) then
begin
UName:=EdtUser.Text;
Upwd:=EdtPassword.Text;
FormMain.Show;
FrmLogin.Hide;
end
else
begin
ShowMessage('请输入正确的密码');
EdtPassword.SetFocus;
exit;
end;
end;
ADOConnection1.ConnectionString:=st;
ADOConnection1.Connected:=true;
必须保证在设计的时候ADOConnection1的Connected为False;否则就会出现上面所说的“对想打开是操作不被允许
”