Solved

Compact and repair

Posted on 2006-11-05
7
849 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

724 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