Click here to Skip to main content
14,576,147 members
Rate this:
Please Sign up or sign in to vote.
See more:
I need to repair and mdb file in c#, i am using the code given here . I have a corrupted mdb file that i need to first recover and then read its data using c# code. Using compact and repair code in above code sample is giving me error unrecognized database format.
Any suggestion?
digimanus 25-Aug-14 9:01am
try to open the file in Access and do the compact and repair there. After that open it via ODBC in c#
Nathan Minier 25-Aug-14 9:08am
Keep in mind that the data corruption could easily be in the DB Metadata and you will never be able to get Access to recognize it as a valid mdb file, or .NET for that matter.

If that's the case, you'll need to read it as streams and dig into the mdb format a bit to attempt to reconcile the metadata and make a baseline repair.
PhilLenoir 25-Aug-14 9:26am
There are deeper repair tools available for MDBs where Access itself can't fix the database. Even if the file can be repaired, you may lose some data. Do you have a recent backup? A combination of fixing the file and compairing to a backup might be necessary.
ShoaibHassanSe 26-Aug-14 1:18am
Thank you for your response. Actually i cant use a repair tool. My requirement is to process the corrupted mdb file and fetch as much data as possible. I was thinking as Nethan told me to read data as streams. But dont know how to proceed on it :(

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1


Use below code

public static void CompactAccessDB(string sFilePath, string sNewFilePath)
                sCompactError = string.Empty;
                object[] oParams;
                object oJRO = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
                oParams = new object[] { string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", sFilePath), string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=5", sNewFilePath) };
                oJRO.GetType().InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, oJRO, oParams);
                System.IO.File.Move(sNewFilePath, sFilePath);
                oJRO = null;
                sCompactError = string.Format("{0} database file successfully compacted.", sFilePath);
            catch (System.Exception ex)
                sCompactError = ex.Message;
ShoaibHassanSe 26-Aug-14 1:24am
its giving me the same error that "Unrecognized format"
CB Sharma 27-Aug-14 10:12am
Can you send the sample database file?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100