0

I'm developing a software that during its start-up will check if the attached local database (by which I mean a separate .mdf file that is attached using an open dialog box) is the appropriate database for the software. And if it is, I'll copy the source file, then paste it where my software can always find it (ex. C:\Program Files(my system generated folder)). To do that, I have to first release the .mdf file so I can copy it to my folder.

How can I release the .mdf file so I can create a copy of it to my desired folder during runtime of my software? I'm using vb.net.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
eric21
  • 3
  • 3
  • possible duplicate of [How to force a SQL Server 2008 database to go Offline](http://stackoverflow.com/questions/3005662/how-to-force-a-sql-server-2008-database-to-go-offline) – Gary Walker Jul 15 '14 at 05:19

1 Answers1

0

Not sure if this is what you actually mean, but it seems likely. To copy / move database files you have to kick every one off i.e., take it offline (or detach it). You can do this via SMSS.

If you are looking to do this via code, this may be what you are looking for.

alter database mydbname set offline with rollback immediate

Note the this kicks everyone off immediately, potential causing unhappy users.

ADDED

After I answered, it occurred to me this had to be a duplicate, and it was

Community
  • 1
  • 1
Gary Walker
  • 8,831
  • 3
  • 19
  • 41
  • Thanks! it actually worked! But what do you mean for "unhappy users" does this line of code could harm your database files or something? – eric21 Jul 15 '14 at 08:58
  • The users might be unhappy if there were working on the database and had their connection terminated suddenly. Presumably you would only drop their connection when it was safe to do so, or you had no choice. – Gary Walker Jul 15 '14 at 13:39