///////////////////// (一)項(xiàng)目文件 test.dPR //////////////////////
program SerialGet;
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 //創(chuàng)建句柄,判斷此應(yīng)用程序是否在運(yùn)行
begin
//調(diào)用全局函數(shù),創(chuàng)建并顯示登陸界面
if doLogin then //登陸成功
begin
Application.CreateForm(TfrmMain, frmMain);
//數(shù)據(jù)模塊文件不須在這兒創(chuàng)建,因?yàn)?ULogin.pas 中已創(chuàng)建
//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; // 全項(xiàng)目公用函數(shù)
function CreateMutex: Boolean; // 全項(xiàng)目公用函數(shù)
procedure DestroyMutex; // 全項(xiàng)目公用函數(shù)
implementation
uses UDataModule; //引用數(shù)據(jù)模塊
var Mutex: hWnd;
{$R *.dfm}
function doLogIn:boolean; //由項(xiàng)目文件調(diào)用此函數(shù)
begin
with TfrmLogin.create(application) do //創(chuàng)建并顯示登陸界面
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:數(shù)據(jù)庫不對 2:沒有此用戶 3:合法
function TfrmLogin.checkPsw:integer;
var name,sPsw,SQL,sValue:string;
begin
Application.CreateForm(TDataModule1, DataModule1); //此處創(chuàng)建了數(shù)據(jù)模塊
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;
///////////////////// (三)數(shù)據(jù)模塊 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; //返回結(jié)果集記錄數(shù)
end;
except
result:=0;
end;
finally
screen.cursor:=old_Cursor;
end;
end;
function DoSQL(s: string;query:TADOQuery):boolean; //運(yùn)行 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;
新聞熱點(diǎn)
疑難解答
圖片精選