Click here to Skip to main content
15,888,124 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all

While restoring the database i am getting the below error

SQL
The file 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\project_blank_blank.mdf' cannot be overwritten.  It is being used by database 'project_blank_blank'.
File 'project_blank_blank' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\project_blank_blank.mdf'. Use WITH MOVE to identify a valid location for the file.
The file 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\project_blank_blank_log.LDF' cannot be overwritten.  It is being used by database 'project_blank_blank'.
File 'project_blank_blank_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\project_blank_blank_log.LDF'. Use WITH MOVE to identify a valid location for the file.
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
RESTORE DATABASE is terminating abnormally.
Changed database context to 'master'.


this is my restore query

SQL
Dim str As New SqlCommand("use master; RESTORE DATABASE " & txtCompanyCode.Text & " FROM disk='" & dest & "'", con)


Please help me out..
Posted

You cannot restore a database till there are connections pending with the old one.
If you are the administrator, you can close these open connections through Management Studio or through code.
 
Share this answer
 
Comments
[no name] 20-May-13 2:53am    
Then is there any other solution..because i have to attach like this..
There could be no other solution except as proposed in Solution 1.

And it make sense, how can the SQL Server perform a Restore operation while the database is being used in some application or service?

If let's suppose that if it allows like this then obviously chances are there that your restored database might be get corrupted or will not function properly.
 
Share this answer
 

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