Solved

Compact and repair

Posted on 2006-11-05
7
812 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 118
COnsume rest client 6 36
Run time Error 4 34
C# parent child form 5 14
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 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

20 Experts available now in Live!

Get 1:1 Help Now