views:

97

answers:

1

hi

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  A: 

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));
Matthew Whited
how to use /compact ? i try c:\MydataBase.mdb /compact - but not work
Gold
@Gold, see the update to my answer.
Matthew Whited
This requires full Access to be installed (I'm not sure if you can compact with the runtime or not). If it's an MDB file, you can use DAO and Jet 4 on any Windows PC from Win2000 on. If it's ACCDB, then you need to install the ACE, and then you can use DAO to do it, with no need for full Access. If full Access 2007/2010 is installed, then you should still use DAO as it's more efficient than running the whole of Access.
David-W-Fenton
@David, I don't disagree. I just provided what he asked for. If you want a sample for DAO you can follow the link I provided above.
Matthew Whited
The link you provided uses JRO, not DAO. JRO is an idiotic library that exists only because of MS's stupid ADO-everywhere campaign. It won't necessarily be installed on every computer, whereas DAO is for Jet 4.
David-W-Fenton
Then feel free to post a link for DAO. I really don't feel like playing with COM right now.
Matthew Whited
I don't do WinForms programming, so I have no idea how to post code for interoperability with COM. The command is DBEngine.CompactDatabase, and I suspect that it takes very little work to set it up, but I can't post the code because this is not my programming environment.
David-W-Fenton
Well unless you have C# 4.0 it's going to be really fun to code and look at (even worse if you do late binding)... but the OP was happy with my answer so I'm going to leave it open for someone else to do the research and coding for the COM/DAO answer.
Matthew Whited
Oh, I think your answer is fine as one possible solution. It just has more outside dependencies than doing it via DAO.
David-W-Fenton