views:

1043

answers:

3

I have a database that is locking mdb's and such that I'd like to backup. However the tool (I have the source) I am using opens the file before backing it up and finds that it is locked.

Is there a way I can open it for read-only purposes?

For reference the tool uses C# and .NET 2.0 (but can be updated to 3.5).

+2  A: 

This is similar to this question:

http://stackoverflow.com/questions/660019/opening-a-files-shadow-copy-if-the-current-copy-is-in-use/660098

It depends on how the database is opening the MDB file. If it's not allowing read sharing then you're out of luck unless you are able to open the shadow copy. There's a discussion on how to do this here:

http://stackoverflow.com/questions/259253/how-do-i-copy-a-file-or-folder-that-is-locked-under-windows-programmatically

Keltex
+5  A: 

The reason your tool locks the file is to prevent changes to the file as it is being backed up. For example, if you begun your backup, but halfway in the DBMS (i.e. SQL Server) decided to make a change to a file, then your backup would be corrupt.

I recommend you use the tools that are provided with your database solutions to perform a backup. The other option is to stop the database before backing it up.

Matthew Timbs
Please read the question in more detail.
Samuel
@Samuel: no u. Matthew's answer is the right one.
Welbog
No, he has it backwards. The database has a exclusive read lock on the file, and the tool cannot get a lock. Matthew has it the other way around.
Samuel
+1 for using the native database backup tools
snemarch
Right, the DB has an exclusive lock on the data file BECAUSE the writers of the DBMS knew that to copy the file while the database is running is not a good idea, so they lock it. This is to prevent you from doing exactly what you're trying to do.
Matthew Timbs
What native backup tools would you use when using a Jet back end, as in this present question? You *did* read it before answering, right?
David-W-Fenton
Why yes, I did read your question. You didn't mention Jet in your original post. I'm not familiar with backup tools used for Jet databases, but the issue is the same no matter the DBMS. Try googling.
Matthew Timbs
+3  A: 

If the DBMS is holding a write lock on the file, and you read it, you're risking the DBMS writing the file as you're reading it. Depending on what part was written, you could end up with a corrupt backup of the file. You're best off reading the file only if the DBMS isn't writing to the file or letting the DBMS handle its own backups.

Welbog