views:

267

answers:

3

Hi all

I want Compact a Access DataBase File in C# by Code. please Help me

Edited: I USe Microsoft Access 2007 DataBase File

+3  A: 

The easiest way is going to be by using the standalone program “JetComp.exe” and just calling that from your c# code

http://download.microsoft.com/download/access2000/utility/1.0/win98me/en-us/jetcu40.exe

Another option would be to open a DAO connection to the database and use the .CompactDatabase method. Not being a c# bod I cant really offer a code sample but it is quite easy to do in VBA, for that reason I would just use the first option

Kevin Ross
I wouldn't recommend JetComp.exe, as it's going to do things beyond what the native Jet CompactDatabase methods will do. That is, you could lose data if the file is damaged. The DAO and JRO CompactDatabase methods will crap out in cases where JetComp will go ahead and try to fix things. I'd keep JetComp as backup for the rare case when the standard DAO/JRO CompactDatabase method errors out because of a damaged file.
David-W-Fenton
+1  A: 

I would use Jet Replication Objects (JRO). For example code, see here.

Foole
+1  A: 

Not a C# programmer...but could you Shell to something like:

c:\myFolder\myAccessDatabase.mdb /compact

This as you can see uses the command line switch /compact.

maxhugen
It's much better to do this kind of thing directly with the data interface libraries that talk to the db engine itself. And your suggestion would require that Access be installed.
David-W-Fenton