///////////////////// (一)项目文件 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 //创建句柄,判断此应用程序是否在运行
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;
本文地址:http://com.8s8s.com/it/it4800.htm