NHibernate: Freeing .mdf file
-
22-07-2019 - |
Question
I'm using NHibernate for a project. This project opens a SQL Server 2005 database and then, after all work is done, It's supposed to backup the database. The problem is that SQL Server keeps the handle open after calling factory.Close(), so the backup copy fails. How do I close the file handle? Thanks in advance.
Solution
If you are trying to make a copy of the database file you can take the database offline.
ALTER DATABASE AdventureWorks SET OFFLINE
--Copy the data file
ALTER DATABASE AdventureWorks SET ONLINE
To take an online backup you can use the BACKUP DATABASE command.
BACKUP DATABASE AdventureWorks
TO DISK = 'Z:\SQLServerBackups\AdvWorksData.bak'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow