Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 644
Pirlo Avatar asked Dec 05 '25 13:12

Pirlo


1 Answers

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
like image 159
HaveNoDisplayName Avatar answered Dec 07 '25 03:12

HaveNoDisplayName



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!