This article describes the method of using late binding to compress Access database in C#. Generally speaking, VB supports Com late binding very well, and can be implemented using reflection in C#. The specific methods are as follows:
The function implementation code is as follows:
public static void CompactAccessDB(string strMdbName) { string TempMdbName = + @"\"; //Create Jet engine object object objJetEngine = (("")); //Set parameter array //Modify the number in "Jet OLEDB:Engine Type=5" according to the Access version you are using. //5 Corresponding to JET4X format (access 2000,2002) object[] objParams = new object[] { ("Provider=.4.0;Data Source={0}",strMdbName), //Input the connection string ("Provider=.4.0;Data Source={0};Jet OLEDB:Engine Type=5",TempMdbName) //Output connection string }; //Calling the CompactDatabase method through reflection ().InvokeMember("CompactDatabase", , null, objJetEngine, objParams); //Delete the original database file (strMdbName); //Rename the compressed database file (TempMdbName, strMdbName); //Release Com component (objJetEngine); objJetEngine = null; }
So why use late binding?You can directly add references to components to the project to implement it.
In a multi-person collaboration development environment, adding a reference requires checking out the project file for modification. If there is no component on the group member machine, she will not be able to compile the modified program.
This method is very convenient, and it can be used by copying the past. There is no need to modify the project.
Although late binding has a small performance loss, the convenience it gets is still cost-effective. And this function is not often used.
How to deal with access files with passwords?
It's very simple, just add the password setting to the input connection string.
In this way, the compressed mdb has no password by default. If you want the compressed mdb to have a password, add the password setting to the output connection string.
This method can actually be used to modify mdb's password, cancel mdb's password, and set a password for mdb.