Compress and sort access database files (mdb) in pb)

Source: Internet
Author: User

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.


Related Article

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.