Как назначить пароль на таблицу?

Материал из DRKB

Как назначить пароль на таблицу?[править | править код]

unit Unit2;

interface

uses
  BDE, SysUtils, DBTables, Windows;

function TablePasswort(var table: TTable; password: string): Boolean;

implementation

function StrToOem(const AnsiStr: string): string;
begin
  SetLength(Result, Length(AnsiStr));
  if Length(Result) > 0 then
    CharToOem(PChar(AnsiStr), PChar(Result))
end;

function TablePasswort(var table: ttable; password: string): Boolean;
var
  pTblDesc: pCRTblDesc;
  hDb: hDBIDb;
begin
  Result := False;
  with table do
  begin
    if Active and (not Exclusive) then
      Close;
    if (not Exclusive) then
      Exclusive := True;
    if (not Active) then
      Open;
    hDb := DBHandle;
    Close
  end;
  GetMem(pTblDesc, SizeOf(CRTblDesc));
  FillChar(pTblDesc^, sizeof(CRTblDesc), 0);
  with pTblDesc^ do
  begin
    StrPCopy(szTblName, StrToOem(table.tablename));
    szTblType := szParadox;
    StrPCopy(szPassword, StrToOem(password));
    bPack := True;
    bProtected := True
  end;
  if DbiDoRestructure(hDb, 1, pTblDesc, nil, nil, nil, False) <> DBIERR_NONE then
    exit;
  if pTblDesc <> nil then
    FreeMem(pTblDesc, sizeof(CRTblDesc));
  Result := True
end;

end.


Пример использования:

TablePasswort(Table1, 'secret');


Source: Взято с Delphi Knowledge Base: http://www.baltsoft.com/
ID: 02940