小弟药品实现权限登录,Unit1是一个2秒的信息显示窗体,在显示登录窗体后,我输入工号和密码后老显示‘工号或密码不正确’
我已经用了2种代码来完成这个工作但都是遇上这个问题,是数据连接不上么??小弟刚接触delphi,希望大侠给小弟点提示~亿分感谢!!登录窗体的代码
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, StdCtrls,ExtCtrls;type
TFormLogin = class(TForm)
Edit1: TEdit;
ComboBox1: TComboBox;
Button1: TButton;
DataSource1: TDataSource;
Query1: TQuery;
Database1: TDatabase;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations } public
{ Public declarations } end;var
FormLogin: TFormLogin;
implementationuses Unit_FormMain;{$R *.dfm}procedure TFormLogin.Button1Click(Sender: TObject);
var
UserID:string;
count:integer; //登录次数
begin
//判断用户输入的工号是否为空
if FormLogin.ComboBox1.Text='' then
begin
ShowMessage('工号不能为空!');
Exit;
end;
//判断用户输入的工号是否为合法工号
FormLogin.Query1.Filter:='ID='''+Formlogin.ComboBox1.Text+'''';
if FormLogin.Query1.Eof then
begin
Showmessage('工号或密码不正确!');
Exit;
end;
Inc(count);
UserID:=ComboBox1.Text;
UserPwd:=Edit1.Text;//Query1.Open;
if Query1.Locate('ID',UserID,[]) then
begin
if Query1.FieldByName('Pwd').AsString=UserPwd then //密码解密,登录成功
begin
UserClass:=Query1.fieldbyname('Class').AsString; //取得权限 self.ModalResult:=mrOK; //关闭窗口并返回mrOK值
end;
end;
if count>=3 then
begin
self.ModalResult:=mrcancel; //只允许登录3次
end;
//Query1.Close;end;procedure TFormLogin.Button2Click(Sender: TObject);
begin
application.Terminate;
end;procedure TFormLogin.FormCreate(Sender: TObject);
begin
//判断Query1中的当前记录是否是最后一个记录
while not Query1.Eof do
begin
//将当前纪录值添加到ComboBox1组合框中
ComboBox1.AddItem(Query1.FieldValues['ID'],sender);
//将当前记录下移一个
Query1.MoveBy(1);
end;
end;end.主窗体的dpr代码
program Project_FormMain;uses
Forms,
Controls,
Unit_FormMain in 'Unit_FormMain.pas' {FormMain},
Unit1 in 'Unit1.pas' {FormSplash},
Unit2 in 'Unit2.pas' {FormLogin};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TFormSplash, FormSplash);
FormSplash.ShowModal;
FormSplash.Free;
FormLogin:=TFormLogin.Create(application); //登录窗口
if FormLogin.ShowModal=mrOK then //登录窗体关闭时返回了mrOK值,说明登录成功 begin
Application.CreateForm(TFormMain, FormMain);
end;
Formlogin.Free;
if FormLogin.ShowModal=mrcancel then
begin
application.Terminate;
end;
Application.Run;
end.
我已经用了2种代码来完成这个工作但都是遇上这个问题,是数据连接不上么??小弟刚接触delphi,希望大侠给小弟点提示~亿分感谢!!登录窗体的代码
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, StdCtrls,ExtCtrls;type
TFormLogin = class(TForm)
Edit1: TEdit;
ComboBox1: TComboBox;
Button1: TButton;
DataSource1: TDataSource;
Query1: TQuery;
Database1: TDatabase;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations } public
{ Public declarations } end;var
FormLogin: TFormLogin;
implementationuses Unit_FormMain;{$R *.dfm}procedure TFormLogin.Button1Click(Sender: TObject);
var
UserID:string;
count:integer; //登录次数
begin
//判断用户输入的工号是否为空
if FormLogin.ComboBox1.Text='' then
begin
ShowMessage('工号不能为空!');
Exit;
end;
//判断用户输入的工号是否为合法工号
FormLogin.Query1.Filter:='ID='''+Formlogin.ComboBox1.Text+'''';
if FormLogin.Query1.Eof then
begin
Showmessage('工号或密码不正确!');
Exit;
end;
Inc(count);
UserID:=ComboBox1.Text;
UserPwd:=Edit1.Text;//Query1.Open;
if Query1.Locate('ID',UserID,[]) then
begin
if Query1.FieldByName('Pwd').AsString=UserPwd then //密码解密,登录成功
begin
UserClass:=Query1.fieldbyname('Class').AsString; //取得权限 self.ModalResult:=mrOK; //关闭窗口并返回mrOK值
end;
end;
if count>=3 then
begin
self.ModalResult:=mrcancel; //只允许登录3次
end;
//Query1.Close;end;procedure TFormLogin.Button2Click(Sender: TObject);
begin
application.Terminate;
end;procedure TFormLogin.FormCreate(Sender: TObject);
begin
//判断Query1中的当前记录是否是最后一个记录
while not Query1.Eof do
begin
//将当前纪录值添加到ComboBox1组合框中
ComboBox1.AddItem(Query1.FieldValues['ID'],sender);
//将当前记录下移一个
Query1.MoveBy(1);
end;
end;end.主窗体的dpr代码
program Project_FormMain;uses
Forms,
Controls,
Unit_FormMain in 'Unit_FormMain.pas' {FormMain},
Unit1 in 'Unit1.pas' {FormSplash},
Unit2 in 'Unit2.pas' {FormLogin};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TFormSplash, FormSplash);
FormSplash.ShowModal;
FormSplash.Free;
FormLogin:=TFormLogin.Create(application); //登录窗口
if FormLogin.ShowModal=mrOK then //登录窗体关闭时返回了mrOK值,说明登录成功 begin
Application.CreateForm(TFormMain, FormMain);
end;
Formlogin.Free;
if FormLogin.ShowModal=mrcancel then
begin
application.Terminate;
end;
Application.Run;
end.
FormLogin.Query1.Filter:='ID='''+Formlogin.ComboBox1.Text+'''';
if FormLogin.Query1.Eof then
begin
Showmessage('工号或密码不正确!');
Exit;
end;
Inc(count);
UserID:=ComboBox1.Text;
UserPwd:=Edit1.Text; //Query1.Open;
if Query1.Locate('ID',UserID,[]) then
begin
if Query1.FieldByName('Pwd').AsString=UserPwd then //密码解密,登录成功
begin
UserClass:=Query1.fieldbyname('Class').AsString; //取得权限 self.ModalResult:=mrOK; //关闭窗口并返回mrOK值
end;
end; 换成
FormLogin.Query1.close;
FormLogin.Query1.sql.clear;
FormLogin.Query1.sql.add('select * from 你的数据表名 where ID='''+Formlogin.ComboBox1.Text+'''');
FormLogin.Query1.open;
if FormLogin.Query1.recordcount>0 then
self.ModalResult:=mrOK
else begin
Showmessage('工号或密码不正确!');
self.ModalResult:=mrCancel;
close;
end;
试试
再有
你count没有初始化,又是在事件内定义 ,Inc(count) 又不在循环体内
每次触发只执行1次 还是个随机的
if count>=3 then 就没有意义了.....
FormLogin.Query1.close;
FormLogin.Query1.sql.clear;
FormLogin.Query1.sql.add('select * from 你的数据表名 where ID='+''''+Formlogin.ComboBox1.Text+'''');
FormLogin.Query1.open;
if FormLogin.Query1.RecordCount>0 then
self.ModalResult:=mrOK
else
begin
Showmessage('工号或密码不正确!');
self.ModalResult:=mrCancel;
close;
end;
Application.Initialize;
Application.CreateForm(TFormSplash, FormSplash);
FormSplash.ShowModal;
//FormSplash.Free;
//这儿去掉
FormLogin:=TFormLogin.Create(application); //登录窗口
if FormLogin.ShowModal=mrOK then //登录窗体关闭时返回了mrOK值,说明登录成功 begin
Application.CreateForm(TFormMain, FormMain);
end else
//Formlogin.Free;你怎么那么喜欢释放呀,窗口关闭时就已经自动释放了,还释放能不出问题吗
//if FormLogin.ShowModal=mrcancel then 你想让登陆窗口显示两次吗,笑话
begin
application.Terminate;
end;
Application.Run;
end.
Forms,
main in 'main.pas' {Form1},
Login in 'Login.pas' {FLogin},
TcpClientThread in 'TcpClientThread.pas',
mytype in '..\Public\mytype.pas',
ConnectType in 'ConnectType.pas',
uCtrl in 'uCtrl.pas';{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TFLogin, FLogin);
FLogin.LoginOk:=false;
FLogin.ShowModal;
if FLogin.LoginOk then
begin
FLogin.Free;
Application.Run;
end else
Application.Terminate;
end.
这是我以前做过的一个关于登陆窗口的,你可以参考一下