如何在 SQL Server Management Studio 中添加数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27768774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:07:46  来源:igfitidea点击:

How to add a database in SQL Server Management Studio?

sqlsql-serverdatabasetsqlmdf

提问by Pirlo

I have been sent a database called StudentsDB.mdf and I want to enter it into my SQL Server Management Studio databases . How to do that ?

我收到了一个名为 StudentsDB.mdf 的数据库,我想将它输入到我的 SQL Server Management Studio 数据库中。怎么做 ?

I want to know if I copy a .mdf file from the directory where are all my databases which is C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA and sent it to another person will he be able to import this database in his SQL Server Management Studio and see the database?

我想知道我是否从 C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA 所在的所有数据库所在的目录中复制一个 .mdf 文件并将其发送给另一个人,他是否能够导入这个数据库在他的 SQL Server Management Studio 中看到的数据库?

回答by HaveNoDisplayName

Try this one

试试这个

Step 1 Right-click “Databases” and click the “Attach” option to open the Attach Databases dialog box.

步骤 1 右键单击​​“数据库”,然后单击“附加”选项以打开“附加数据库”对话框。

Step 2 Click the “Add” button to open the Locate Database Files dialog box.

步骤 2 单击“添加”按钮打开“定位数据库文件”对话框。

Step 3 Type in the full name of the .MDF file, including the full device and directory path, as the following example illustrates: c:\StudentsDB.mdf Click the "OK" button. SQL Server Management Studio loads the database from the .MDF file.

步骤3 输入.MDF 文件的全名,包括完整的设备和目录路径,如下例所示: c:\StudentsDB.mdf 单击“确定”按钮。SQL Server Management Studio 从 .MDF 文件加载数据库。

OR

或者

Step 1 Click “New Query” in the Management Studio's main toolbar.

步骤 1 在 Management Studio 的主工具栏中单击“新建查询”。

Step 2 Type a Create Database statement using the following Transact-SQL code:

步骤 2 使用以下 Transact-SQL 代码键入创建数据库语句:

CREATE DATABASE MyDatabase ON 
(FILENAME = 'c:\StudentsDB.mdf'), 
(FILENAME = ' c:\StudentsDB.ldf') FOR ATTACH;

Step 3 Click the “Execute” button in the Transact-SQL toolbar. SQL Server Management Studio restores the database.

步骤 3 单击 Transact-SQL 工具栏中的“执行”按钮。SQL Server Management Studio 还原数据库。

OR

或者

CREATE DATABASE StudentDB ON
(FILENAME = N'C:\StudentsDB.mdf')
FOR ATTACH_REBUILD_LOG
GO

回答by M.Ali

Execute the following command from SSMS.

从 SSMS 执行以下命令。

USE master;
GO

EXEC sp_attach_single_file_db @dbname   = N'StudentsDB'
                             ,@physname = N'D:\<path to mdf file>\StudentsDB.mdf'
GO

Now if you refresh the database list in SSMS it should show a database StudentsDBin the list.

现在,如果您刷新 SSMS 中的数据库列表,它应该会StudentsDB在列表中显示一个数据库。

回答by Joel Coehoorn

I want to know if I copy a *.mdf file ... and [send] it to another person, will he be able to import this database?

我想知道如果我复制一个 *.mdf 文件...并[发送]给另一个人,他能导入这个数据库吗?

You can do this, but there are a few considerations. The first is that you need to take the database offline, or use another command to ensure there are no pending transactions waiting to be written or locks or latches waiting to be closed.

您可以这样做,但有一些注意事项。第一个是您需要使数据库脱机,或使用另一个命令来确保没有等待写入的挂起事务或等待关闭的锁或闩锁。

The second consideration is that, once the database is imported, you may need to recreate (by hand or by script) a few items that aren't stored within the mdf file itself. This includes users and permissions, links to other databases, and other services that are provided by at the Server level rather than the Database level.

第二个注意事项是,一旦导入数据库,您可能需要(手动或通过脚本)重新创建一些未存储在 mdf 文件本身中的项目。这包括用户和权限、到其他数据库的链接以及在服务器级别而不是数据库级别提供的其他服务。