views:

85

answers:

5

hi

I tried to compress my Access 2007 database like this:

System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\Office12\msaccess.exe",@"c:\Mydb.mdb /compact "); 

In my C# program and I get this error:

You attempted to open a database that is already opened exclusively by user on machine . Try again when the database is available. (Error 3356)

I tried Conn.close() but still get the error

Thanks in advance.

+2  A: 

Check your Task Manager to see if you have Access already open. If you do, close these instances and retry.

Ardman
what to look for ? a dont see access in Task Manager
Gold
You are looking to see if MSAccess.EXE is already running. When using Microsoft Office applications with C#, I've always found that I can't exit the application gracefully without leaving an executable still running in the Task Manager.
Ardman
It's not necessarily MSACCESS.EXE that has the file open. Any program utilizing Jet could have it open.
David-W-Fenton
A: 

Maybe something went wrong and you just have an extra *.ldb file out there. Go to where your DB is stored:

c:\Mydb.ldb

If no one is using it delete the file.

Nix
thank's for the help but unhelpful - same error
Gold
A: 

Hi,

//Edit begin

Check out these basic troubleshooting items:

http://office.microsoft.com/en-us/access-help/troubleshoot-compacting-repairing-or-recovering-an-access-file-HP005188316.aspx?queryid=728e1007c19b43bba069cb7f11364f7a&respos=0&CTT=1

See if any of the above reasons are causing the compact to fail!

//Edit end

Check the following link: http://support.microsoft.com/kb/209207

Use /excl with the code to open it exclusively.

So code would be:

System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\Office12\msaccess.exe",@"c:\Mydb.mdb /excl /compact "); 

By using the excl option you will be opening it exclusively!

Hope it helps!

Vaibhav
thank's for the help but unhelpful - same error
Gold
Vaibhav
There is no such thing as a separate "repair" process since Jet 3.51, c. 1997. A repair happens as part of the compact process if the file's internal structures are found to be corrupt. So, there is really only COMPACT to worry about.
David-W-Fenton
A: 

The way compact & repair works in Access is it compacts to a new file, deletes the original file and then renames. You can use the DAO Engine to do the same thing. As far as I can tell from MS Visual C# 2010 Express, you can set a reference to DAO and use that:

var MyDBE=new DAO.DBEngine();
MyDBE.CompactDatabase("c:\\docs\\Mybe.mdb", "c:\\docs\\temp.mdb");

To ensure the database is not in use, you can check if the .ldb exists.

I am sorry, but I do not know anything about c#, so these notes are very crude.

Remou
The existence of an LDB file does not mean the file is open, because the LDB file can be left after the last user closes. There are a number of scenarios where that happens, including but not limited to if the user lacks DELETE permissions on the folder. If the LDB is not there, it means nobody has it open, but if it *is* there, it only suggests that somebody may have it open, rather than proving it. One way to check would be to try to delete the LDB file if it's there, but then you'd be trapping the resulting error, so you might as well just trap the compact fail error.
David-W-Fenton
+2  A: 

Maybe connection pooling is the issue? Not completely sure where connection pooling is implemented (I believe that this is just part of the provider e.g. SQL server and therefore might not be relevant to this issue) But it will keep connections open between the client and server even thougth the connection object is set to "closed". You can clear a sql server connection pool using conn.ClearPool What are you using for the provider to connect to your database? (OLEDB?) If so try ReleaseObjectPool()

MikeG
I had this problem just 5 minutes ago, although instead of explicitly calling `OleDbConnection.ReleaseObjectPool()`, I set the connection string parameter `OLE DB Services=-2`, which explicitly disables connection pooling.
Rabid