Re: detach database file from local SQL Server Express instance by C#?



After the database is attached SQL Server Express opens database files with exclusive access and you cannot do a copy. To release the exclusive access you have to detach the database. This is done using the sp_detach_db system stored procedure:
http://msdn2.microsoft.com/en-us/library/ms188031.aspx

Note that in order for detach to work there should be no user connections to the database. You can obtain exclusive rights to the database when all users disconnect with something like this:

ALTER DATABASE DatabaseName SET SINGLE_USER;

If you need to force users out of the database immediately, you can use something like this (note, this will roll back incomplete user transactions):

ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
or
ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK AFTER <integer> [SECONDS];

The second statement will give you an option to roll back after the specified number of seconds (substitute <integer> with seconds).

More for working with SQL Server Express instances here:
http://msdn2.microsoft.com/en-us/library/bb264564.aspx

More on ALTER DATABASE:
http://msdn2.microsoft.com/en-us/library/ms174269.aspx


HTH,

Plamen Ratchev
http://www.SQLStudio.com

.



Relevant Pages

  • Re: Problem with VBA created subform in Access 2007
    ... machine not be seeing the delay in the DB. ... User-authorization for exclusive database access. ... default open mode set to exclusive access, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Exclusive Access Conflict
    ... it's a simple sql command to populate a listbox on a form. ... "You do not have exclusive access to the database at this time. ... If they have Adobe Acrobat Writer 7.0 installed, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Exclusive Access Conflict
    ... everytime) with the following message: ... "You do not have exclusive access to the database at this time. ... If they have Adobe Acrobat Writer 7.0 installed, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Cant run dynamic crosstab queries after setting up security
    ... After I set up my "employee" group for my standard users who are only allow to enter basic data through an input form and run some reports, I can't get some crosstab queries to work in a form that dynamically changes the crosstab query definition based on the criteria entered by the user. ... Have your system administrator to establish the appropiate permissions" Any ideas what type of permissions I need? ... I am also getting a message everytime these standard users open the database and the switchboard form comes up that tells them "You don't have exclusive access to the database at this time. ...
    (microsoft.public.access.security)
  • Re: Database Restore
    ... database from within a Win32 Delphi application? ... Exclusive access could not be obtained because the database is in use. ... RESTORE DATABASE is terminating abnormally. ... The thing is that these same parameters work fine in SSMS. ...
    (borland.public.delphi.database.ado)