How do you stop a user-instance of Sql Server? (Sql Express user instance database files locked, eve

Posted by Bittercoder on Stack Overflow See other posts from Stack Overflow or by Bittercoder
Published on 2010-03-02T23:22:50Z Indexed on 2010/03/08 17:06 UTC
Read the original article Hit count: 367

Filed under:
|
|
|
|

When using SQL Server Express 2005's User Instance feature with a connection string like this:

<add name="Default" connectionString="Data Source=.\SQLExpress;
  AttachDbFilename=C:\My App\Data\MyApp.mdf;
  Initial Catalog=MyApp;
  User Instance=True;
  MultipleActiveResultSets=true;
  Trusted_Connection=Yes;" />

We find that we can't copy the database files MyApp.mdf and MyApp_Log.ldf (because they're locked) even after stopping the SqlExpress service, and have to resort to setting the SqlExpress service from automatic to manual startup mode, and then restarting the machine, before we can then copy the files.

It was my understanding that stopping the SqlExpress service should stop all the user instances as well, which should release the locks on those files. But this does not seem to be the case - could anyone shed some light on how to stop a user instance, such that it's database files are no longer locked?


Update

OK, I stopped being lazy and fired up Process Explorer. Lock was held by sqlserver.exe - but there are two instances of sql server:

sqlserver.exe  PID: 4680  User Name: DefaultAppPool
sqlserver.exe  PID: 4644  User Name: NETWORK SERVICE

The file is open by the sqlserver.exe instance with the PID: 4680

Stopping the "SQL Server (SQLEXPRESS)" service, killed off the process with PID: 4644, but left PID: 4680 alone.

Seeing as the owner of the remaining process was DefaultAppPool, next thing I tried was stopping IIS (this database is being used from an ASP.Net application). Unfortunately this didn't kill the process off either.

Manually killing off the remaining sql server process does remove the open file handle on the database files, allowing them to be copied/moved.

Unfortunately I wish to copy/restore those files in some pre/post install tasks of a WiX installer - as such I was hoping there might be a way to achieve this by stopping a windows service, rather then having to shell out to kill all instances of sqlserver.exe as that poses some problems:

  1. Killing all the sqlserver.exe instances may have undesirable consequencies for users with other Sql Server instances on their machines.
  2. I can't restart those instances easily.
  3. Introduces additional complexities into the installer.

Does anyone have any further thoughts on how to shutdown instances of sql server associated with a specific user instance?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sqlexpress