c# compact and repair access database

Hi

I have a requirement to allow the user to do a 'compact and repair' from the system I have developed.  I have looked at some code online and tried the following:

using JRO;


private void CompactDatabase(string Path, string Database, string Password="")
        {
            
            string Database2 = Database + "1";
            string oldmdbfile = "";
            string newmdbfile = "";

            if (Password != "")
            {
                oldmdbfile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + Database + ";Persist Security Info=True;Jet OLEDB:Database Password='" + Password + "';";
                newmdbfile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + Database2 + ";Persist Security Info=True;Jet OLEDB:Database Password='" + Password + "';";
                
            }
            else
            {
                oldmdbfile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + Database + ";";
                newmdbfile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + Database2 + ";";

            }

            string oldmdbfilepath = Path +  Database;
            string newmdbfilepath = Path + Database2;

            JRO.JetEngine engine = new JetEngine();
            engine.CompactDatabase(oldmdbfile, newmdbfile);
            File.Delete(oldmdbfilepath);
            File.Move(newmdbfilepath, oldmdbfilepath);
            MessageBox.Show("Database compact and repaired successfully !");

        }

Open in new window


but this is failing with the following message

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

Any ideas why this is failing? or is there a better method?

Cheers

CD
cycledudeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Is the database  you want to compact open / in use ?  (If there is an ldb file in the same folder with the same name as the mdb file then it probably is open/in use).
0
cycledudeAuthor Commented:
nope, not in use
0
cycledudeAuthor Commented:
may/may not be relevant, but the database is in access 2003 format
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

AndyAinscowFreelance programmer / ConsultantCommented:
Does this repair have to be done by code ?  (You do have extra options if you can do things by hand).
0
cycledudeAuthor Commented:
I would prefer to do allow the user to be able to do it from a 'button click' as otherwise I will need to give someone the password... which I am non to keen to do.
0
cycledudeAuthor Commented:
For future searchers, I have a solution but it is using DAO... so I am not sure this will work with later than access 2003 databases...

Add a reference to Microsoft DAO

now use this code:

 private void CompactTheDatabase(string Path, string Database, string Password="")
        {
            string filePath = Path + Database;
            string compactFile = Path + "temp.mdb";
            
            try
            {
                
                if (File.Exists(Path + Database))
                {
                    dao.DBEngine db = new dao.DBEngine();
                    db.CompactDatabase(filePath, compactFile,null,null,";pwd=" + Password);
                }

                if (File.Exists(compactFile))
                {
                    File.Delete(filePath);
                    File.Move(compactFile, filePath);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

Open in new window


this successfully compacts my database for me.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cycledudeAuthor Commented:
this is a working solution to the problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.