Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please brothers I want to compress and repair my accdb database..

I search and found this code but I don't how to use it ?

C#
public static void CompactAndRepair(string accessFile, Microsoft.Office.Interop.Access.Application app)
        {
           string tempFile = Path.Combine(Path.GetDirectoryName(accessFile),
                              Path.GetRandomFileName() + Path.GetExtension(accessFile));

            app.CompactRepair(accessFile, tempFile, false);
            app.Visible = false;

            FileInfo temp = new FileInfo(tempFile);
            temp.CopyTo(accessFile, true);
            temp.Delete();
        }
Posted
Comments
Andy Lanng 27-May-15 6:44am    
looks pretty straight forward. whats the issue?

pass the file path and an application reference and it does the work for you.

I don't know your code. How can I say anything more.

Include the code in your application, and call it from a Button Click event handler.
Pass it the full path (for example: @"D:\Temp\My Databases\MyDB.accdb") in the first parameter, and an instance of Access in the second:
C#
Microsoft.Office.Interop.Access.Application msAccess = new Microsoft.Office.Interop.Access.Application();


Wait. Probably a long time...
 
Share this answer
 
There is an article[^] on CodeProject that could help.
BTW the sample code you found need some more fixings. It copy your access database to a temporary file, then copy it back to the original file. But maybe you want to check if the database was successfully repaired before copying it back destroying even the original copy ...
And you may want to catch any exception to correctly handle the conversion.
Information about the CompactRepair() method can be found here[^].
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900