The method functions are provided below:
1. Declare an external function to obtain the temporary path of the system.
FUNCTION ulong GetTempPath (ulong nBufferLength, ref string lpBuffer) LIBRARY "kernel32.dll" alias for "GetTempPathA"
2. Core methods:
Global type gf_CompactAndRepairAccessDB from function_object
End type
Forward prototypes
Global function integer gf_compactandrepairaccessdb (string as_mdb)
End prototypes
Global function integer gf_compactandrepairaccessdb (string as_mdb );
// Check whether the database file exists
If not FIleExists (as_MDB) then return-1
// Create a temporary file name
String ls_tmpFIle
Ls_tmpFIle = Space (255)
GetTempPath (255, ls_tmpFIle)
Ls_tmpFIle + = "temp. mdb"
// Create DBEngine
Integer result
Oleobject ole_DBEngine
Ole_DBEngine = create oleobject
Result = ole_DBEngine.connecttonewobject ("JRO. JetEngine ")
// Creation failed
If result <> 0 then return result
// Use DBEngine to compress database files
String sProvider = 'provider = Microsoft. Jet. OLEDB.4.0 ;'
String sOldMDB, sNewMDB
SOldMDB = sProvider + 'data Source = '+ as_MDB
SNewMDB = sProvider + 'data Source = '+ ls_tmpFIle
Ole_DBEngine.CompactDatabase (sOldMDB, sNewMDB)
Destroy ole_DBEngine
// Copy the temporary database file that has just been compressed to the original location
FIleCopy (ls_tmpFIle, as_MDB, true)
// Delete a temporary file
FileDelete (ls_tmpFIle)
Return 0
End function
3. Call Method
Gf_compactandrepairaccessdb ("E: \ eNo \ collect. mdb ")
It should be noted that no other operations can be performed to open the database during database compression and restoration; otherwise, the Operation will fail. Therefore, close the connection to the database first, otherwise, an error is returned.
In addition, this function can be modified to implement the database backup function, which is simpler and will not be provided here.