This code compacts and repairs an MS Access database from a C# .NET application
Introduction
This code compacts and repairs an MS Access database from a C# .NET application, no matter if it's a simple ".mdb", or a ".mdw"-secured workgroup-shared DB. It performs exactly the same operation as "Tools - Database Utils - Compact and Repair Database..." menu item in the MS Access application. The code uses "late binding" (creating COM-objects in memory at runtime), and that's why you don't need any annoying interop COM references in your project. You don't even need MS Office installed. Just make sure you have a Jet Engine (Jet is included in MDAC package, which comes with any Windows installation starting from NT 4).
Background
Don't you hate COM-library references in .NET-projects? I believe that pure .NET-code has to be free of any interops, RCWs, and other referenced COM-stuff. Basically because there's a load of different versions of MS libraries (for example, MS Office Object Library 9, 10, 11 etc.). We never know what version of MS Office is installed on a client machine, that's why we should access a COM-object via ProgID, and not CLSID. For example: you want to be sure, that when you call for an "Excel.Application", you get Excel, no matter what version of MS Office is installed. And when you add a reference "MS Excel 10 Object library", you add a strong limitation to your software! So... use System.Reflection and late binding.
Using the code
Just call a method CompactAccessDB. This method compacts and repairs your database.
Parameters:
connectionString - connection string to your database.
mdwfilename - is a full name (path+name) of an MDB-file you want to compact and repair.
Due to Jet limitations, the method compacts your database to a new file, so we have to copy the new compacted file over an old one.
When you call this method, make sure that there's no open connections to your database. Stop your threads.
Now, to the code:
This code compacts and repairs an MS Access database from a C# .NET application
This code compacts and repairs an MS Access database from a C# .NET application
/**//// <summary>
This code compacts and repairs an MS Access database from a C# .NET application
/// MBD compact method (c) 2004 Alexander Youmashev
This code compacts and repairs an MS Access database from a C# .NET application
/// !!IMPORTANT!!
This code compacts and repairs an MS Access database from a C# .NET application
/// !make sure there's no open connections
This code compacts and repairs an MS Access database from a C# .NET application
/// to your db before calling this method!
This code compacts and repairs an MS Access database from a C# .NET application
This code compacts and repairs an MS Access database from a C# .NET application
/// </summary>
This code compacts and repairs an MS Access database from a C# .NET application
/// <param name="connectionString">connection string to your db</param>
This code compacts and repairs an MS Access database from a C# .NET application
/// <param name="mdwfilename">FULL name
This code compacts and repairs an MS Access database from a C# .NET application
/// of an MDB file you want to compress.</param>
This code compacts and repairs an MS Access database from a C# .NET application
public static void CompactAccessDB(string connectionString, string mdwfilename)
This code compacts and repairs an MS Access database from a C# .NET application
This code compacts and repairs an MS Access database from a C# .NET application
This code compacts and repairs an MS Access database from a C# .NET application
{
This code compacts and repairs an MS Access database from a C# .NET application
object[] oParams;
This code compacts and repairs an MS Access database from a C# .NET application
This code compacts and repairs an MS Access database from a C# .NET application
//create an inctance of a Jet Replication Object
This code compacts and repairs an MS Access database from a C# .NET application
object objJRO =
This code compacts and repairs an MS Access database from a C# .NET application