Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
restore cannot process database 'bw' because it is in use
by this session. It is recommended that the master database
be used when performing this operation.
restore database is terminating abnormally


Note: Here 'bw' is my DB name.,
Posted

Try to run your restore operation in master database. It looks like you have selected the "BW" database while running the restore operation.

The below command will select the master database and then you can execute the restore database command.
USE Master
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 10-Sep-12 2:20am    
5'ed, :D
__TR__ 10-Sep-12 2:27am    
Thanks :)
Umapathi K 10-Sep-12 2:28am    
str = "USE MASTER RESTORE DATABASE bw FROM DISK = 'c:\\SQLBackup\\bw.bak' WITH MOVE 'bw' TO 'c:\\SQLBackup\\bw.mdf' , MOVE 'bw_Log' TO 'C:\\SQLBackup\\bw.ldf', REPLACE ";

i used above codig but its not restoring
__TR__ 10-Sep-12 2:52am    
Are you executing your SQL statements in a query window?
Try prefixing your restore with:
SQL
USE MASTER
GO
<your restore statements>
At present, I suspect that you are opening a new query window on the DB you want to restore which will put an implicit
SQL
USE BW
GO
in front of the query.
If the DB is in use, it cannot be restored to.
 
Share this answer
 
Comments
Umapathi K 10-Sep-12 2:09am    
str = "USE MASTER RESTORE DATABASE bw FROM DISK = 'c:\\SQLBackup\\bw.bak' WITH MOVE 'bw' TO 'c:\\SQLBackup\\bw.mdf' , MOVE 'bw_Log' TO 'C:\\SQLBackup\\bw.ldf', REPLACE ";

i used above coding but not restoring
Try SMO way of doing this:
SQL Server 2008 - Backup and Restore Databases using SMO[^]

More detailed examples:
http://www.mssqltips.com/sqlservertip/1849/backup-and-restore-sql-server-databases-programmatically-with-smo/[^]

Note:*****Yes! you have dependancy on MS SQL Server(definitely) if you choose to do this way.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900