简体   繁体   中英

How to add a database in SQL Server Management Studio?

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 ?

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?

Try this one

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

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

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.

OR

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

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

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.

OR

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

Execute the following command from 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 StudentsDB in the list.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM