ADO Database Delphi

procedure packParadoxTable(Table: TTable; pack: boolean; PWD: string);
var isActive : boolean;
TblDesc : CRTblDesc; // Specific information about the table structure, indexes, etc.
hDb : hDbiDb; // Uses as a handle to the database
CURDB : TDataBase;
TablePath : array[0..dbiMaxPathLen] of char; // Path to the currently opened table }
s : string;
begin
try
try check(DbiInit(nil)) except end;
isActive := Table.Active;
if not isActive then try Table.Open; except end;
// Set Table Descriptions Record
FillChar(TblDesc, SizeOf(CRTblDesc), 0); // Initialize the table descriptor
with TblDesc do
begin
bPack := pack; // Set the packing option
szTblType := szPARADOX;
//StrCopy(szTblType, GetTableType); // Place the table type in descriptor
S := Table.TableName;
StrCopy(TblDesc.szTblName, @S);
// AnsiToNative(Locale: TLocale; const AnsiStr: string; NativeStr: PChar; MaxLen: Integer): PChar;
// Use Locale info convert the Native Str; since, [ç,Ç,ı,İ,ğ,Ğ,ö,Ö,ş,Ş,ü,Ü, ' '] might be used in TableName
AnsiToNative(Table.Locale, Table.TableName, szTblName, SizeOf(szTblName) - 1); // Place the table name in descriptor
bProtected := PWD <> '';
if bProtected then //StrPCopy(szPassword, PWD);
AnsiToNative(Table.Locale, PWD, szPassword, SizeOf(szPassword) - 1); // Place the table name in descriptor
end;
hDb := nil; // Initialize the DB handle
CURDB := Session.FindDataBase(Table.DataBaseName);
if CURDB = nil then CURDB := Session.OpenDatabase(Table.DataBaseName);
if CURDB <> nil then hDb := CURDB.Handle;
if CURDB = nil then if Table.DataBase <> nil then hDb := Table.DataBase.Handle;
if CURDB <> nil
then hDb := CURDB.Handle
else begin
// Get the current table's directory. This is why the table MUST be opened until now
try
S := AddBackSlash(GetAliasPath(Table.DataBaseName));
if Table.DBHandle <> nil
then Check(DbiGetDirectory(Table.DBHandle, True, TablePath))
else if DirectoryExists(S)
then StrPCopy(TablePath, S)
else StrPCopy(TablePath, ExtractFilePath(Table.TableName))
except
if DirectoryExists(S)
then StrPCopy(TablePath, S)
else StrPCopy(TablePath, ExtractFilePath(Table.TableName))
end;
Table.Close; // Close the table
// NOW: since the DbiDoRestructure call needs a valid DB handle BUT the table cannot be opened,
// call DbiOpenDatabase to get a valid handle. Just leaving Active = FALSE does not give you a valid handle
Check(DbiOpenDatabase(@Table.DataBaseName, 'STANDARD', dbiReadWrite, dbiOpenExcl, nil, 0, nil, nil, hDb));
Check(DbiSetDirectory(hDb, TablePath)); // Set the table's directory to the old directory
end;
Table.Close; // Close the table
try
Check(DbiDoRestructure(hDb, 1, @TblDesc, nil, nil, nil, FALSE)); // Pack the PARADOX table
except
end;
if CURDB = nil then Check(DbiCloseDatabase(hDb)); // Close the temporary database handle
if isActive then Table.Open; // Re-Open the table
except
end;
end;