How to use a program to compress an access (*. mdb) Database

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.