views:

190

answers:

1

The MSDN documentation for SQLEXPRESS says:

When an application first establishes a connection from a running instance of SQL Server Express, SQL Server Express will automatically attach an .mdf file. When the user closes the application, SQL Server Express detaches the .mdf file from the instance.

This does not appear to be happening. If I replace the MDF file with a new one of the same name (after deleting the log file, of course) SQL Server Express will refuse to attach it.

I've tried just about every combination of connection string parameters possible, and it's driving me crazy. Any suggestions?

+1  A: 

The detach/close does happen. If it wouldn't happen then you could not possibly replace the MDF file, because it would be in use. The documentation you quote is not entirely accurate. The correct documentation is at SQL Server 2005 Express Edition User Instances:

  • User instance databases have the Auto Close option set so that if there are no connections to a database for 8-10 minutes, the database shuts down and the file is closed. This happens automatically, but it can take a while, especially if connection pooling is enabled for your connections.
  • Detaching the database from the instance by calling sp_detach_db will close the file. This is the method Visual Studio uses to ensure that the database file is closed when the IDE switches between user instances.

If I'd venture a guess I'd say that the database is not detached but auto-closed, and replcing the MDF after deleting the LDF will be (rightfully) seen as an error when trying to open the database.

As side notes:

  • One should never ever delete the LDF file. If you want to replce the database, replace both the MDF and the LDF with the new ones.
  • Make sure you replace with proper MDF and LDF versions. SQL Server can upgrade a database, but can never downgrade it.
  • Get the error. If SQL Express refuses to attach a database, it will give a reason. Look into the RANU created ERRORLOG (in the user profile), the systen event log, or attach profiler to the user instance.
Remus Rusanu
This is at least partially incorrect. (1) I am not using User Instances (they are deprecated), and (2) The XCOPY deployment documentation I referenced specifically states to NOT distribute/copy the LDF file.That said, yes, the symptoms seem to match that the database is closed but not detached. Which is not what Microsoft says it does. Calling 'sp_detach_db' under all circumstances under which the application could close seems like a pretty ugly workaround.
chris
Sry, I assumed you use AttachDBFilename since the overwhelming majority of users do that w/o realising the draw-backs.
Remus Rusanu
Yes, I am using AttachDBFilename, since that is how "XCOPY Deployment" works. However, that is an entirely separate issue from RANU.
chris
I wasn't aware in 2K8 the AtachDBFileName does not start a user isntance, as it did in 2005. If the 2008 xcopy attaches the db with auto-close and *leaves it attached* on disconnect (but auto-closed) then this would explain the behavior you see. Why do you need to detach and replace as a routine operation in your application? I undertsand at development time, but on deployment? At development time forcing a sp_detach_db seems acceptable, is what VS does under the covers anyway apparently.
Remus Rusanu
In testing (and production, on the analysis side) we often need to swap in a different database file to work from a known baseline. Unfortunately this looks like a needlessly complicated implementation on the part of Microsoft.
chris