?
Solved

Compact and repair

Posted on 2006-11-05
7
Medium Priority
?
859 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Accepted Solution

by:
muzzy2003 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

777 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