Solved

Compact and repair

Posted on 2006-11-05
7
808 Views
Last Modified: 2012-06-21

Hi using c# 2.0 in win form, building a small app for user to easily repair access backend,

im using the article on
http://www.codeproject.com/cs/database/mdbcompact_latebind.asp

to assist but getting an error 'Exception has been thrown by the target of an invocation' point to this

 objJRO.GetType().InvokeMember("CompactDatabase",
                    System.Reflection.BindingFlags.InvokeMethod,
                    null,
                    objJRO,
                    oParams);

this is what ive done:

private void cmd_recover_Click_1(object sender, EventArgs e)
        {
            string mdwfilename;
            OleDbConnection conn;
            mdwfilename = txt_filelocation.Text;
            conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdwfilename + ";Persist Security Info=True;Password=**;User ID=***;Mode=Read;Jet OLEDB:System database=E:/Admin/Client Work//XP_sys.mdw");

           
                CompactAccessDB(conn, mdwfilename);
         
            MessageBox.Show("Done");
        }



 public static void CompactAccessDB(OleDbConnection conn, string mdwfilename)
        {

           


                object[] oParams;

                //create an inctance of a Jet Replication Object
                object objJRO =
                  Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
             
                //filling Parameters array
                //(yes, jetengine5 is for JET4X, no misprint here)
                oParams = new object[] {



        conn,
        "Provider=Microsoft.Jet.OLEDB.4.0;Data" +
        " Source=C:\\tempdb.mdb;Jet OLEDB:Engine Type=5"};

                //invoke a CompactDatabase method of a JRO object
                //pass Parameters array
                objJRO.GetType().InvokeMember("CompactDatabase",
                    System.Reflection.BindingFlags.InvokeMethod,
                    null,
                    objJRO,
                    oParams);

                //database is compacted now
                //to a new file C:\\tempdb.mdw
                //let's copy it over an old one and delete it

                System.IO.File.Delete(mdwfilename);
                System.IO.File.Move("C:\tempdb.mdb", mdwfilename);

                //clean up (just in case)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
                objJRO = null;
               
           
           

            //MessageBox.Show("Repair Completed", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

Any Ideas???
0
Comment
Question by:ziwez0
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17877786
The first parameter should be a connection string, not an open connection. In fact, the open connection would block the compact I suspect even if the parameter worked. So, in your click event handler just construct the connection string and pass this as a string to the compact method.
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17909982
Hi. Did this sort your problem out?
0
 
LVL 2

Author Comment

by:ziwez0
ID: 17912615
Hi, havent given it ago yet, will do later on today;

but you mean i should do something like this..


string newstring = "c:/somelocation/"
newstring,
        "Provider=Microsoft.Jet.OLEDB.4.0;Data" +
        " Source=C:\\tempdb.mdb;Jet OLEDB:Engine Type=5"};


0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 16

Accepted Solution

by:
muzzy2003 earned 500 total points
ID: 17912639
Something like this:

private void cmd_recover_Click_1(object sender, EventArgs e)
        {
            string mdwfilename;
            mdwfilename = txt_filelocation.Text;
            string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdwfilename + ";Persist Security Info=True;Password=**;User ID=***;Mode=Read;Jet OLEDB:System database=E:/Admin/Client Work//XP_sys.mdw";

           
                CompactAccessDB(connString, mdwfilename);
         
            MessageBox.Show("Done");
        }



 public static void CompactAccessDB(string connString, string mdwfilename)
        {

           


                object[] oParams;

                //create an inctance of a Jet Replication Object
                object objJRO =
                  Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
             
                //filling Parameters array
                //(yes, jetengine5 is for JET4X, no misprint here)
                oParams = new object[] {



        connString,
        "Provider=Microsoft.Jet.OLEDB.4.0;Data" +
        " Source=C:\\tempdb.mdb;Jet OLEDB:Engine Type=5"};

                //invoke a CompactDatabase method of a JRO object
                //pass Parameters array
                objJRO.GetType().InvokeMember("CompactDatabase",
                    System.Reflection.BindingFlags.InvokeMethod,
                    null,
                    objJRO,
                    oParams);

                //database is compacted now
                //to a new file C:\\tempdb.mdw
                //let's copy it over an old one and delete it

                System.IO.File.Delete(mdwfilename);
                System.IO.File.Move("C:\tempdb.mdb", mdwfilename);

                //clean up (just in case)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
                objJRO = null;
               
           
           

            //MessageBox.Show("Repair Completed", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
0
 
LVL 2

Author Comment

by:ziwez0
ID: 17912761
Nice one, ill give it a go when I get home in a few hours, thanks for that
0
 
LVL 2

Author Comment

by:ziwez0
ID: 17916979
Ok still get this

System.Reflection.TargetInvocationException was unhandled
  Message="Exception has been thrown by the target of an invocation."

Pointing to this:
objJRO.GetType().InvokeMember("CompactDatabase",
                System.Reflection.BindingFlags.InvokeMethod,
                null,
                objJRO,
                oParams);
0
 
LVL 2

Author Comment

by:ziwez0
ID: 17917070
opps got it, slight spelling mistake
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsā€¦
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a feā€¦
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now