Import .bak file to a database in SQL server

前端 未结 12 1751
悲哀的现实
悲哀的现实 2020-12-22 17:51

I have a file with .bak extension.

How can I import this date to a database in SQL Server?

相关标签:
12条回答
  • 2020-12-22 17:51

    You can use node package, if you often need to restore databases in development process.

    Install:

    npm install -g sql-bak-restore
    

    Usage:

    sql-bak-restore <bakPath> <dbName> <oldDbName> <owner>
    

    Arguments:

    • bakpath, relative or absolute path to file
    • dbName, to which database to restore (!! database with this name will be deleted if exists !!)
    • oldDbName, database name (if you don't know, specify something and run, you will see available databases after run.)
    • owner, userName to make and give him db_owner privileges (password "1")

    !! sqlcmd command line utility should be in your PATH variable.

    https://github.com/vladimirbuskin/sql-bak-restore/

    0 讨论(0)
  • 2020-12-22 17:56

    On SQL Server Management Studio

    1. Right click Databases on left pane (Object Explorer)
    2. Click Restore Database...
    3. Choose Device, click ..., and add your .bak file
    4. Click OK, then OK again

    Done.

    0 讨论(0)
  • 2020-12-22 17:56

    You can simply restore these database backup files using native SQL Server methods, or you can use ApexSQL Restore tool to quickly virtually attach the files and access them as fully restored databases.

    Disclaimer: I work as a Product Support Engineer at ApexSQL

    0 讨论(0)
  • 2020-12-22 17:59

    After opening and logging in to MS SQL Server Management studio

    1. Right-click on Databases
    2. Choose Restore Database
    3. Choose Device and click on ...
    4. Click Add and select the .bak file you want to import
    5. Click OK

    You're done!

    0 讨论(0)
  • 2020-12-22 18:02

    Although it is much easier to restore database using SSMS as stated in many answers. You can also restore Database using .bak with SQL server query, for example

    RESTORE DATABASE AdventureWorks2012 FROM DISK = 'D:\AdventureWorks2012.BAK'
    GO
    

    In above Query you need to keep in mind about .mdf/.ldf file location. You might get error

    System.Data.SqlClient.SqlError: Directory lookup for the file "C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL.1\MSSQL\DATA\AdventureWorks.MDF" failed with the operating system error 3(The system cannot find the path specified.). (Microsoft.SqlServer.SmoExtended)

    So you need to run Query as below

    RESTORE FILELISTONLY 
    FROM DISK = 'D:\AdventureWorks2012.BAK'
    

    Once you will run above Query you will get location of mdf/ldf use it Restore database using query

    USE MASTER
    GO
    RESTORE DATABASE DBASE 
    FROM DISK = 'D:\AdventureWorks2012.BAK'
    WITH 
    MOVE 'DBASE' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DBASE\MSSQL\DATA\DBASE.MDF',
    MOVE 'DBASE_LOG' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DBASE\MSSQL\DATA\DBASE_1.LDF', 
    NOUNLOAD,  REPLACE,  NOUNLOAD,  STATS = 5
    GO
    

    Source:Restore database from .bak file in SQL server (With & without scripts)

    0 讨论(0)
  • 2020-12-22 18:04

    On Microsoft SQL Server Management Studio 2019:

    On Restore Database window:

    1. Choose Device

    2. Choose Add and pick target file

    3. OK to confirm

    4. OK to confirm restore

    0 讨论(0)
提交回复
热议问题