views:

308

answers:

1

In .NET, you can open a SQL Express database simply by attaching the database MDF file in the connection string (that is, you don't have to have any server software installed, it just attaches to the MDF file on the fly).

When an application does this, does it obtain an exclusive lock on the database file? Or, can a second application also attach to and open the MDF file in this same way, and query the database while program #1 is using it?

+1  A: 

You are correct, only a single connection can be open to the MDF at a time.

This can get really annoying when you open the database through your server explorer in Visual Studio, and then try to run your application.

I've caught myself doing that too many times to count, and I always invariably end up just attaching the mdf to my sql server instance.

Joseph
Man - that sucks. Seems like it would be a pain to use this feature at all. I wonder if most people just go ahead and install the SQL Express server, which I suppose would give you better control over things like jobs, backups, etc. anyway.
Sam Schutte
@Sam You hit the nail on the head. I basically never use the file path in the connection string anymore. It's way more convenient to just attach the database to a sql express instance. And since sql express is free, it's only taking the time to install it, which isn't that long.
Joseph
If I have a situation where a database is opened in this manner by program 1, I wonder if it would work to:1. Copy the attached database to another file (DB2.mdf)2. Now open the copied database with program 2Usually a copy works on opened objects like that...hmmm...
Sam Schutte

related questions