public void CompactAccessDB(string connectionString, string newFile, string password)
{
string tempName = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Templates),
@"~tempdb.mdb");
string tempDestConnection = string.Format(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" +
@"Jet OLEDB:Encrypt Database=true;Jet OLEDB:Database Password={1}",
tempName,
password
);
object[] myParams;
object jro = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
myParams = new object[]{
connectionString,
tempDestConnection
};
jro.GetType().InvokeMember("CompactDatabase",
System.Reflection.BindingFlags.InvokeMethod,
null,
jro,
myParams);
System.IO.File.Delete(newFile);
System.IO.File.Move(tempName, newFile);
System.Runtime.InteropServices.Marshal.ReleaseComObject(jro);
jro = null;
}
浙公网安备 33010602011771号