SQL Server:从 .mdf 导入数据库?

发布于 2024-09-29 20:07:21 字数 253 浏览 5 评论 0原文

我的本地盒子上有一个 .mdf 文件。

我的本地机器上安装了 SQL Server 2008 Express 和 SQL Management Studio 2008 Express。

我究竟如何将此 .mdf 文件作为新数据库导入到我的 SQL Server 中?

这似乎是一项极其常见的任务,必须在全球范围内每天执行数千次,而且我不知道如何在 Management Studio Express 中执行此操作。

我缺少什么?

I have an .mdf file on my local box.

I have SQL Server 2008 Express and SQL Management Studio 2008 Express installed on my local box.

How in the world do I import this .mdf file as a new database into my SQL Server?

This seems like a ridiculously common task that must be performed thousands of times a day across the globe, and I cannot figure out how to do it in Management Studio Express.

What am I missing?

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

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

发布评论

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

评论(6

野味少女 2024-10-06 20:07:21

打开 SQL Management Studio Express 并登录到要附加数据库的服务器。在“对象资源管理器”窗口中,右键单击“数据库”文件夹并选择“附加...”“附加数据库”窗口将打开;在该窗口中单击“添加...”,然后导航到您的 .MDF 文件并单击“确定”。再次单击“确定”完成数据库附加,您就完成了。该数据库应该可供使用。
此致 :)

Open SQL Management Studio Express and log in to the server to which you want to attach the database. In the 'Object Explorer' window, right-click on the 'Databases' folder and select 'Attach...' The 'Attach Databases' window will open; inside that window click 'Add...' and then navigate to your .MDF file and click 'OK'. Click 'OK' once more to finish attaching the database and you are done. The database should be available for use.
best regards :)

阳光下的泡沫是彩色的 2024-10-06 20:07:21

如何:将数据库文件附加到 SQL Server Express

请参阅 通过 sqlcmd 访问数据库:

sqlcmd -S Server\Instance

然后发出命令:

USE [master]
GO
CREATE DATABASE [database_name] ON 
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\<database name>.mdf' ),
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\<database name>.ldf' )
 FOR ATTACH ;
GO

See: How to: Attach a Database File to SQL Server Express

Login to the database via sqlcmd:

sqlcmd -S Server\Instance

And then issue the commands:

USE [master]
GO
CREATE DATABASE [database_name] ON 
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\<database name>.mdf' ),
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\<database name>.ldf' )
 FOR ATTACH ;
GO
明月夜 2024-10-06 20:07:21

要执行此操作,请参阅下图:

在此处输入图像描述

下一步是添加 *.mdf 文件,

非常重要,.mdf 文件必须位于 C:......\MSSQL12 .SQLEXPRESS\MSSQL\DATA

在此处输入图像描述

现在删除日志文件

在此处输入图像描述

To perform this operation see the next images:

enter image description here

and next step is add *.mdf file,

very important, the .mdf file must be located in C:......\MSSQL12.SQLEXPRESS\MSSQL\DATA

enter image description here

Now remove the log file

enter image description here

若水微香 2024-10-06 20:07:21

除了上面 @daniele3004 发布的答案中提到的步骤之外,我还必须以管理员身份打开 SSMS,否则它会显示“主文件是只读”错误。

转到“开始”菜单,导航到 SSMS 链接,右键单击 SSMS 链接,选择以管理员身份运行。然后执行上述步骤。

Apart from steps mentioned in posted answers by @daniele3004 above, I had to open SSMS as Administrator otherwise it was showing Primary file is read only error.

Go to Start Menu , navigate to SSMS link , right click on the SSMS link , select Run As Administrator. Then perform the above steps.

请远离我 2024-10-06 20:07:21

如果您没有 LDF 文件,则:

1) 将 MDF 放入 C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\

2) 在 ssms 中,转到 <代码>数据库->附加并添加 MDF 文件。它不会让您以这种方式添加它,但它会告诉您其中包含的数据库名称。

3) 确保您运行 ssms.exe 的用户有权访问此 MDF 文件。

4) 现在您知道了 DbName,请运行

EXEC sp_attach_single_file_db @dbname = 'DbName', 
@physname = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\yourfile.mdf';

参考:https://dba.stackexchange.com/问题/12089/attaching-mdf-without-ldf

If you do not have an LDF file then:

1) put the MDF in the C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\

2) In ssms, go to Databases -> Attach and add the MDF file. It will not let you add it this way but it will tell you the database name contained within.

3) Make sure the user you are running ssms.exe as has acccess to this MDF file.

4) Now that you know the DbName, run

EXEC sp_attach_single_file_db @dbname = 'DbName', 
@physname = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\yourfile.mdf';

Reference: https://dba.stackexchange.com/questions/12089/attaching-mdf-without-ldf

随风而去 2024-10-06 20:07:21

在 docker 中托管 MS SQL 时,如下所示

version: '3.4'    
services:
  sqlserver:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: sqlserver
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=Samle123$
    ports:
      - "1433:1433"
    volumes:
      - S:\Database\data:/var/opt/mssql/data

  • mdfldf(选项)文件添加到 data 文件夹(那里: S:\Database\data)
  • 在 SSMS 附加 数据库中并选择复制的 mdf 文件。

When hosting MS SQL in docker like:

version: '3.4'    
services:
  sqlserver:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: sqlserver
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=Samle123$
    ports:
      - "1433:1433"
    volumes:
      - S:\Database\data:/var/opt/mssql/data

it's enough to:

  • add mdf and ldf(option) files to data folder (there: S:\Database\data)
  • in SSMS Attach database and selecting copied mdf file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文