Buscar

Verificar se um registro está travado

Código


//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;
   
 
 

Publicidade

Vote na dica




Quantidade de votos: 0 votos
Aceitação: 0%


Detalhes da dica

Categoria: Banco de dados
Adicionada dia: 30/07/03
Por: Carlos Renato Pereira Zen
Visualizada: 3620 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009