Restore a sql server backup to a localdb instance

By eidias on (tags: localdb, categories: tools)

Here’s how to restore a db backup taken from sql server to a localdb instance without using sql server management studio

   1: ALTER DATABASE [<db name>] SET SINGLE_USER WITH ROLLBACK IMMEDIATE 
   2:  
   3: RESTORE DATABASE [<db name>]
   4: FROM DISK = '<path to backup file>'
   5: WITH
   6:     MOVE '<db file name>' TO <path to target db file (.mdf)>',
   7:     MOVE '<db log file name>' TO '<path to target log file (.ldf)>', 
   8:     STATS=5,
   9:     REPLACE

To get the file names you can run sp_helpfile system procedure.