uses
Forms,
UMain in 'UMain.pas' {frmMain},
ULogin in 'ULogin.pas' {frmLogin},
UDataModule in 'UDataModule.pas' {DataModule1: TDataModule},
{$R *.res}
begin
Application.Initialize;
if CreateMutex then //創建句柄,判斷此應用程序是否在運行
begin
//調用全局函數,創建並顯示登陸界面
if doLogin then //登陸成功
begin
Application.CreateForm(TfrmMain, frmMain);
//數據模塊文件不須在這兒創建,因為 ULogin.pas 中已創建
//Application.CreateForm(TDataModule1, DataModule1);
Application.Run;
end else //登陸不成功
begin
try
DataModule1.free;
Application.terminate;
except
end;
end;
end else
begin
DestroyMutex; //釋放句柄
end;
end.
//////////////// (二)登陸窗體 ULogin.pas ULogin.dfm //////////////////
unit ULogin;
interface
uses ......
type
... ... ...
private
function checkPsw:integer;
public
end;
var
frmLogin: TfrmLogin;
function doLogIn:boolean; // 全項目公用函數
function CreateMutex: Boolean; // 全項目公用函數
procedure DestroyMutex; // 全項目公用函數
implementation
uses UDataModule; //引用數據模塊
var Mutex: hWnd;
{$R *.dfm}
function doLogIn:boolean; //由項目文件調用此函數
begin
with TfrmLogin.create(application) do //創建並顯示登陸界面
begin
//窗體的ShowModal屬性
if ShowModal = mrok then result := true else result := false;
free;
end;
end;
procedure DestroyMutex;
begin
if Mutex <> 0 then CloseHandle(Mutex);
end;
function CreateMutex: Boolean;
var
PrevInstHandle: THandle;
APPTitle: PChar;
begin
APPTitle := StrAlloc(100);
StrPCopy(APPTitle, Application.Title);
Result := True;
Mutex := Windows.CreateMutex(nil, False, APPTitle);
if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin
Result := False;
SetWindowText(Application.Handle, '');
PrevInstHandle := FindWindow(nil, APPTitle);
if PrevInstHandle <> 0 then begin
if IsIconic(PrevInstHandle) then
ShowWindow(PrevInstHandle, SW_RESTORE)
else
BringWindowToTop(PrevInstHandle);
SetForegroundWindow(PrevInstHandle);
end;
if Mutex <> 0 then Mutex := 0;
end;
StrDispose(APPTitle);
end;
// -1: 密碼不對 1:數據庫不對 2:沒有此用戶 3:合法
function TfrmLogin.checkPsw:integer;
var name,sPsw,SQL,sValue:string;
begin
Application.CreateForm(TDataModule1, DataModule1); //此處創建了數據模塊
if not DataModule1.ConnOK then
begin result := 1; exit; end;
name := lowercase(editName.text); //文本框
sPsw := lowercase(editPass.text); //文本框
sql := 'select * from maker where name="'+name+'"';
if openSQL(SQL,DataModule1.dsDataSet) <=0 then
begin result := 2; exit; end;
DataModule1.dsDataSet.First ;
sValue := lowercase(DataModule1.dsDataSet.fIEldbyName('loginPsw').asString);
if sValue<>sPsw then result := -1 else result := 3;
end;
///////////////////// (三)數據模塊 UDataModule.pas //////////////////////
... ... ... ...
type
public
ConnOK:boolean;
end;
var
DataModule1: TDataModule1;
function OpenSQL(s: string;query:TADODataSet):integer;
function DOSQL(s: string;query:TADOQuery):boolean;
implementation
{$R *.dfm}
procedure TDataModule1.DataModuleCreate(Sender: TObject); //連接ADOConnection
var SQL,pwd:string;
begin
try
pwd := 'deliSerial';
SQL := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+
extractfilepath(paramstr(0))+'SerialInfo.mdb'+
';Persist Security Info=False;' +
'Jet OLEDB:Database PassWord="'+pwd+'"';
ADOConnection1.Connected := false;
ADOConnection1.ConnectionString := SQL;
ADOConnection1.Connected := true;
ConnOK:=true;
except
ConnOK:=false;
end;
end;
function OpenSQL(s: string;query:TADODataSet):integer; //查詢SQL
var old_Cursor:TCursor;
begin
old_Cursor:=screen.cursor;
screen.cursor:=crSQLWait;
try
try
with query do
begin
close; commandtext:=s; open;
result:=query.recordcount; //返回結果集記錄數
end;
except
result:=0;
end;
finally
screen.cursor:=old_Cursor;
end;
end;
function DOSQL(s: string;query:TADOQuery):boolean; //運行 SQL
var old_Cursor:TCursor;
begin
result:=true;
old_Cursor:=screen.cursor;
screen.cursor:=crSQLWait;
try
try
with query do
begin
close; SQL.Clear; SQL.Add(s); ExecSQL;
end;
except
result:=false;
end;
finally
screen.cursor:=old_Cursor;
end;
end;