Solved

Compact and repair

Posted on 2006-11-05
7
837 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What Does This C# Code Block Do? 5 74
defining NULL or 0 10 51
LINQ Select all records by MAX data syntax help 1 32
Nested forach loop to linq 3 30
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…
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!
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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