Restore Database from MDF and LDF Files in SQL Server 2017/2014/2012

You can restore database from MDF and LDF files in SQL Server 2017, 2014, 2012 in three ways when your SQL Server goes wrong or when you install a new SQL server. You can make it through SQL Server Management Studio, T-SQL, or using EaseUS MS SQL Recovery.

Updated by Daisy on Nov 27, 2024
Written by Cedric
 | Home > MS SQL Database Recovery

Your SQL Server may break down due to some unexpected reason, such as MDF, NDF, or LDF file corruption, system failure, malware attack, etc. At this point, you can make your SQL Server run again by restoring database from MDF and LDF files. In this article, we will show you how to manually restore SQL Server database from MDF and LDF files through SQL Server Management Studio and T-SQL. Or using EaseUS MS SQL Recovery tool to restore SQL database with simple clicks instead of manually entering the command lines.

Part 1. Manually Restore Database from MDF and LDF Files in SQL Server 

There are two ways to restore database from MDF and LDF files in SQL Server 2017/2014 either with SQL Server Management Studio or SQL Server itself. Whichever method you will use, you need to make the following prerequisites.

Tip
The following methods have technical requirements. If you are new to SQL Server and know little about it, you'd better use a simpler way to restore database from MDF and LDF files in SQL Server through EaseUS MS SQL Recovery in Part 2.
  • Detach the database, or there will be error prompting.
  • Put the MDF file and LDF file in the same specific location, like C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\.

Method 1. Using SQL Server Management Studio

1.Open SSMS and go to "Object Explorer".

2.Right-click the database you need to restore and select "Attach". Click the "Add" button when "Attach Database" Windows appears.

3. Browse the location of MDF file and select it. Then, click "OK". And SQL Server Management Studio will restore the database from the MDF file.

Method 2. Using T-SQL

Login to SQL Server database, go to New Query Window and run the following T-SQL script to attach MDF file in SQL Server.

Create database dbname
On
(
Filename= 'path where you copied files',
Filename ='path where you copied log'
)
For attach;

If the above two solutions can't restore SQL Server database from MDF and NDF files, don't get discouraged. You can still use the following error-free way to do it with ease.

Part 2. Restore Database from MDF and LDF Files in SQL Server with SQL Recovery Tool

When your SQL Server database becomes corrupt, usually the MDF files are affected. You can restore your SQL database by repairing the corrupted MDF file with a professional SQL recovery program. And EaseUS MS SQL Recovery is such software that enables you to restore database from MDF file in SQL Server with a few clicks. Meanwhile, it can automatically fix a corrupted log file that can result in SQL database errors while it repairs.

Now, follow the step by step guide given below to restore database from MDF and LDF files in SQL Server 2017, 2014, or 2012. 

Step 1. Stop MS SQL Server service via services.msc or Management Studio.

Step 2. Run EaseUS SQL Recovery. In the main interface, choose the MDF/NDF file of the database you want to restore. Then click "Repair" to start repairing your MDF/NDF file. 

If you know the exact location of the file, click "Browse" to locate the database.

If you don't know the file location, click "Search" to search for the .mdf or .ndf file in.

How to restore database from MDF file in SQL Server - Step 2

Step 3. When it has done, you will see the recovered database objects listed in the left pane of the window.

How to restore database from MDF file in SQL Server - Step 3

Step 4. Click "Export" in the bottom right corner of the screen to save your database objects. Choose a preferred format, MDF, or SQL scripts.

On the "Export to database" window, choose "Create new database" or "Export to existing database" to save the repaired data. If you want "Create new database", enter the database name and choose an SQL location. If you select "Exporting to existing database", you need to select the existing database.

How to restore database from MDF file in SQL Server - Step 4

Step 5. Now restart the SQL Server.

Was This Page Helpful?

 

EaseUS MS SQL Recovery

  • Restore (.mdf and.ndf) Files
  • Recover Corrupted Objects
  • Fix Database Log Files
TRY IT NOW