将 .bak 文件导入 SQL Server 中的数据库

发布于 2024-08-06 21:01:30 字数 74 浏览 5 评论 0原文

我有一个扩展名为 .bak 的文件。

如何将此文件数据导入到 SQL Server 数据库中?

I have a file with .bak extension.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

温馨耳语 2024-08-13 21:01:30

SQL Server Management Studio上,

  1. 右键单击左侧窗格(对象资源管理器)上的数据库
  2. 单击还原数据库...
  3. 选择设备 >,单击 ...,然后添加您的 .bak 文件
  4. 单击确定,然后单击确定< /strong> 再次

完成。

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.

谁对谁错谁最难过 2024-08-13 21:01:30

这将向您显示 DB.bak 中包含的数据库文件列表:

RESTORE FILELISTONLY 
FROM DISK = 'D:\3.0 Databases\DB.bak' 

您将需要该列表中的逻辑名称来进行第二步中的 MOVE 操作:

RESTORE DATABASE YourDB
FROM DISK = 'D:\3.0 Databases\DB.bak' 

并且您必须移动适当的 mdf、ndf & ; ldf 文件使用

With Move 'primarydatafilename' To 'D:\DB\data.mdf', 
Move 'secondarydatafile' To 'D:\DB\data1.ndf', 
Move 'logfilename' To 'D:\DB\log.ldf'

This will show you a list of database files contained in DB.bak:

RESTORE FILELISTONLY 
FROM DISK = 'D:\3.0 Databases\DB.bak' 

You will need the logical names from that list for the MOVE operation in the second step:

RESTORE DATABASE YourDB
FROM DISK = 'D:\3.0 Databases\DB.bak' 

and you have to move appropriate mdf,ndf & ldf files using

With Move 'primarydatafilename' To 'D:\DB\data.mdf', 
Move 'secondarydatafile' To 'D:\DB\data1.ndf', 
Move 'logfilename' To 'D:\DB\log.ldf'
虫児飞 2024-08-13 21:01:30

您可以使用本机 SQL Server 方法简单地还原这些数据库备份文件,也可以使用 ApexSQL Restore 工具快速虚拟附加文件并将它们作为完全恢复的数据库进行访问。

免责声明:我在 ApexSQL 担任产品支持工程师

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

郁金香雨 2024-08-13 21:01:30

不要选择“还原数据库...”,而是选择“还原文件和文件组...”,

然后输入数据库名称,选择 .bak 文件路径作为源,选中还原复选框,然后单击“确定”。如果 .bak 文件有效,它将起作用。

(对于一项非常简单的任务来说,SQL Server 恢复选项名称并不直观。)

Instead of choosing Restore Database..., select Restore Files and Filegroups...

Then enter a database name, select your .bak file path as the source, check the restore checkbox, and click Ok. If the .bak file is valid, it will work.

(The SQL Server restore option names are not intuitive for what should a very simple task.)

怼怹恏 2024-08-13 21:01:30

在 Microsoft SQL Server Management Studio 2019 上:

在此处输入图像描述

在“还原数据库”窗口中:

  1. 选择设备

  2. 选择添加并选择目标文件

  3. 确定确认

  4. 确定恢复

在此处输入图像描述

On Microsoft SQL Server Management Studio 2019:

enter image description here

On Restore Database window:

  1. Choose Device

  2. Choose Add and pick target file

  3. OK to confirm

  4. OK to confirm restore

enter image description here

会傲 2024-08-13 21:01:30
  1. 连接到要存储数据库的服务器
  2. 右键单击​​数据库
  3. 单击恢复
  4. 选择源部分下的设备单选按钮
  5. 单击添加。
  6. 导航到存储 .bak 文件的路径,选择它并单击“确定”
  7. 输入数据库的目标 输入
  8. 要用于存储数据库的名称
  9. 单击“确定”

完成

  1. Connect to a server you want to store your DB
  2. Right-click Database
  3. Click Restore
  4. Choose the Device radio button under the source section
  5. Click Add.
  6. Navigate to the path where your .bak file is stored, select it and click OK
  7. Enter the destination of your DB
  8. Enter the name by which you want to store your DB
  9. Click OK

Done

偏爱自由 2024-08-13 21:01:30

只需使用

sp_restoredb '您的数据库名称' ,'您要恢复的位置'

示例: sp_restoredb 'omDB','D:\abc.bak'

Simply use

sp_restoredb 'Your Database Name' ,'Location From you want to restore'

Example: sp_restoredb 'omDB','D:\abc.bak'

各自安好 2024-08-13 21:01:30

尽管正如许多答案中所述,使用 SSMS 恢复数据库要容易得多。
您还可以使用 .bak 和 SQL Server 查询来恢复数据库,例如

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

在上面的查询中,您需要记住 .mdf/.ldf 文件位置。
你可能会得到错误

System.Data.SqlClient.SqlError:文件“C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL.1\MSSQL\DATA\AdventureWorks.MDF”的目录查找失败,操作系统错误 3(系统无法找到指定的路径。)。 (Microsoft.SqlServer.SmoExtended)

因此,您需要运行如下查询

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

一旦您运行上述查询,您将获得 mdf/ldf 的位置,使用它使用查询恢复数据库

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

源:从 SQL Server 中的 .bak 文件恢复数据库(带和不带脚本)

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)

年华零落成诗 2024-08-13 21:01:30

如果开发过程中经常需要恢复数据库,可以使用node包。

安装:

npm install -g sql-bak-restore

用法:

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

参数:

  • bakpath,文件的相对或绝对路径
  • dbName,要恢复到哪个数据库(!!如果存在此名称的数据库将被删除!!)
  • oldDbName,数据库名称(如果您不知道,请指定一些内容 )并运行,运行后您将看到可用的数据库。)
  • 所有者,要创建的用户名并赋予他 db_owner 权限(密码“1”)

!! sqlcmd 命令行实用程序应位于您的 PATH 变量中。

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文