主页 > 编程 > delphi >

delphi2010 unidac mysql 登录模块代码

2019-03-08 19:09 阅读:165 来源:智宇SEO自媒体

  delphi好玩是好玩,但无奈学习的资料太少太少。曾经的编程界王者,已经落寞的不成样子。

  摸索着把超市管理系统的登录模块写完。有个地方不完美,如果用户名和密码错误,登录窗口会关闭掉。只能重新打开,再登录。这个问题等待解决,今天就这样了。

delphi2010 unidac  mysql 登录模块代码

  工程文件代码:

program Project3;

uses
  Forms,
  Unit4 in 'Unit4.pas' {ShopMain},
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Login};

{$R *.res}
begin
  Application.Initialize;
  Application.CreateForm(Tlogin, login);
  login.ShowModal;
  if login.loginok then
  begin
    login.Free;
    Application.CreateForm(TShopMain, ShopMain);
    Application.Run;
  end
  else
    Application.Terminate;
end.

  登录模块代码:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, MemDS, DBAccess, Uni;

type
  TLogin = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Button2: TButton;
    UniConnection1: TUniConnection;
    UniQuery1: TUniQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    loginok:boolean;
  end;

var
  Login: TLogin;

implementation

uses Unit4;

{$R *.dfm}

procedure TLogin.Button1Click(Sender: TObject);
var uname:string;
var pwd:string;
var sqlstr:string;
begin

  uname:=trim(Edit1.Text);
  pwd:=trim(Edit2.Text);
  sqlstr:= 'select * from admin where uname='''+uname+''' and pwd='''+pwd+'''';
 if (length(uname)>0) or (length(pwd)>0) then
 begin
     with TUniQuery.Create(nil) do
     try
     Connection:= UniConnection1 ;
     Close;
     SQL.Clear;
     SQL.Add(sqlstr);
     Open;
     if RecordCount > 0 then
     begin
     showmessage('登录成功');
     loginok:=true;
     end
     else
     begin
     loginok:=false;
     showmessage('用户名或者密码错误');
     end;
     finally
     Free;
     end;
     Close;
 end
 else
 begin
   showmessage('请输入登录用户名和密码!');
 end;

end;

procedure TLogin.Button2Click(Sender: TObject);
begin
  close;
end;

end.