quarta-feira, 23 de setembro de 2009

Verificar se um registro está travado

//Verificar se o registro está travado

//Inclua a unit DBITYPES na clausula uses do seu form.

function TForm1.IsRecordLocked(Table: TTable; ByAnyone: boolean): Boolean;

var
Locked: BOOL;
hCur: hDBICur;
rslt: DBIResult;
begin
Table.UpdateCursorPos;
// Is the record locked by the current session...
Check(DbiIsRecordLocked(Table.Handle, Locked));
Result := Locked;
// If the current session does not have a lock and the ByAnyone varable is
// set to check all sessions, continue check...
if (Result = False) and (ByAnyone = True) then
begin
// Get a new cursor to the same record...
Check(DbiCloneCursor(Table.Handle, False, False, hCur));
try
// Try and get the record with a write lock...
rslt := DbiGetRecord(hCur, dbiWRITELOCK, nil, nil);
if rslt <> DBIERR_NONE then
begin
// if an error occured and it is a lock error, return true...
if HiByte(rslt) = ERRCAT_LOCKCONFLICT then
Result := True
else
// If some other error happened, throw an exception...
Check(rslt);
end
else
// Release the lock in this session if the function was successful...
Check(DbiRelRecordLock(hCur, False));
finally
// Close the cloned cursor...
Check(DbiCloseCursor(hCur));
end;
end;
end;

//Utilize a função assim:


procedure TForm1.Button1Click(Sender: TObject);
begin
If IsRecordLocked(Table1,True) then
Showmessage('Registro Travado!');
end;

Nenhum comentário:

Postar um comentário