The following function is used to compress the access database. You need to add the ComObj unit functionCompactDatabase (AFileName, APassWord: string): boolean. Compress and fix the database and overwrite the source file constSConnectionStringPRoviderMicrosoft. jet. OLEDB.4.0; DataSource % s; + JetOLEDB: D
The following function is used to CompactDatabase (AFileName, APassWord: string) of the ComObj unit function CompactDatabase (AFileName, APassWord: string) to be added to the access Database: boolean; // CompactDatabase is compressed and repaired to overwrite the source file const sconnection. jet. OLEDB.4.0; Data Source = % s; '+ 'Jet OLEDB: D
The following functions are used
CompressionAccess
DatabaseComObj unit needs to be added
Function CompactDatabase (AFileName, APassWord: string): boolean;
//
CompressionAnd repair
DatabaseOverwrite the source file
Const
SConnectionString = 'provider = Microsoft. Jet. OLEDB.4.0; Data Source = % s ;'
+ 'Jet OLEDB: Database Password = % s ;';
Var
SPath, SFile: Array [0 .. 254] Of Char;
STempFileName: String;
JE: OleVariant;
Begin
GetTempPath (40, SPath); // obtain the Temp path of Windows
GetTempFileName (SPath ,'~ CP ', 0, SFile); // obtain the Temp file name. Windows automatically creates a 0-byte file.
STempFileName: = SFile; // PChar-> String
DeleteFile (STempFileName); // Delete the 0-Byte File Created in Windows
Try
JE: = CreateOleObject ('jro. JetEngine '); // create an OLE object. The OLE object is automatically released when the function ends.
OleCheck (JE. CompactDatabase (format (SConnectionString, [AFileName, APassWord]),
Format (SConnectionString, [STempFileName, APassWord]); //
Compression
Database
// Copy and overwrite the source
DatabaseFile. If the copy fails, the function returns false,
CompressionSuccessful but not functional
Result: = CopyFile (PChar (STempFileName), PChar (AFileName), false );
DeleteFile (STempFileName); // delete a temporary file
Except
Result: = false ;//
CompressionFailed
End;
End;