When using the instance function of SQL Server Express 2005 with a connection string as follows:
<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 cannot copy the database files MyApp.mdf and MyApp_Log.ldf (because they are blocked) even after the SqlExpress service is stopped and are forced to resort to setting up the SqlExpress service from automatic to manual start mode, and then restart the computer before we can copy the files.
It was my understanding that stopping the SqlExpress service should also stop all user instances that should release these file locks. But this does not seem to be the case - can anyone shed some light on how to stop the user instance so that its database files are no longer blocked?
Update
OK, I stopped being lazy and activated Process Explorer. The lock was performed by sqlserver.exe - but there are two instances of the sql server:
sqlserver.exe PID: 4680 User Name: DefaultAppPool sqlserver.exe PID: 4644 User Name: NETWORK SERVICE
File opened by sqlserver.exe instance with PID: 4680
Stopping the SQL Server (SQLEXPRESS) service, killed the process using PID: 4644, but left only PID: 4680.
Seeing that the owner of the remaining process was DefaultAppPool, I tried to stop IIS (this database is used from an ASP.Net application). Unfortunately, this also did not kill the process.
Manually killing the remaining sql server process, deletes the open file descriptor in the database files, allowing them to copy / move.
Unfortunately, I want to copy / restore these files in some installation tasks before / after installing the WiX installer - so I was hoping this could be achieved by stopping the Windows service and not in order to kill all instances of sqlserver.exe, since this creates some problems:
- Killing all instances of sqlserver.exe can have undesirable consequences for users with other instances of Sql Server on their machines.
- I cannot restart these instances easily.
- Introduces additional complexity to the setup program.
Does anyone have any additional thoughts on how to close SQL server instances associated with a particular user instance?
sql file locking sql-server-express wix
Bittercoder
source share