Click here to Skip to main content
12,252,418 members (58,651 online)
Rate this:
 
Please Sign up or sign in to vote.
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 9-Sep-12 20:00pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
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?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try prefixing your restore with:
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
USE BW
GO
in front of the query.
If the DB is in use, it cannot be restored to.
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 10 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100