0
votes

my C# winforms program work's on database access 2007

this database became swollen.

is there any way for compress and fix this database by C# code ?

if i do it manual (through access) it became less swollen

thank's in advance

1
Will Access be installed on the computers this app runs on?Matthew Whited
Is it an MDB or an ACCDB? If the former, all the tools you need are installed on all copies of Windows starting with Win2000 -- you've got Jet 4 and DAO available to you to do the compact. If it's ACCDB, you need to install the drivers if you don't have full Access installed.David-W-Fenton

1 Answers

1
votes

You could use the /compact command line argument for msaccess.exe or you can use interop and do something like this Compact And Repair that I found on code project.

C# sample using MS Access Command Line...

var mdbFileName = Path.GetFullPath("youraccessdb.mdb");
if (!File.Exists(mdbFileName))
    throw new FileNotFoundException(
        "Could not find Access Database",
        mdbFileName);

var programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
var accessPath = Path.Combine(
                    programFiles,
                    @"Microsoft Office\Office12\MSACCESS.EXE");

if (!File.Exists(accessPath))
    throw new FileNotFoundException(
        "Could not find MSACCESS.EXE",
        accessPath);

var commandArgs = string.Format("/compact \"{0}\"", mdbFileName);

var process = Process.Start(accessPath, commandArgs);
process.WaitForExit();

if (process.ExitCode != 0)
    throw new ApplicationException(string.Format(
        "Access Exited with Error Code [{0}]",
        process.ExitCode));