Restore- Attach mdf data base to new database in sql

Restore database through query in sql

Just try to execute following query to restore database using query analyzer or from network location.

 RESTORE DATABASE testdb  
 FROM DISK = 'C:\folder1\testdb.bak'  
  WITH REPLACE,  
  MOVE 'testdb_Data' TO 'C:\folder1\testdb.mdf',  
  MOVE 'testdb_Log' TO 'C:\folder1\testdb.LDF'  


If you want to restore database by code then you can try following SQL Code to restore database .


 Use master  
 go  
 RESTORE DATABASE [SID] FROM disk = N'\\SERVERNAME\SFILE.bak' WITH FILE = 1, NORECOVERY, REPLACE, NOUNLOAD, STATS = 10  
 go  
 RESTORE LOG [SID] FROM DISK = N'\\SERVERNAME\SFILE.trn' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 10  
 GO  
 sp_change_users_login @Action='Report'  

Attach mdf data base to new database in sql

To Attach mdf data base to new database in sql You can follow following steps.

1 Go to enterprise manager, expand tree till Database
2 Right click on Database and select 'Attach Database'
3 Browse for your .MDF file and select it.
4 You can change store Location of file to your Dir.
5 Change your Database Name if you want.
6 Select DB Owner. by default it is 'SA'
7 Now you can get your data in newly created Database