Due to unforeseen reasons like corruption in MDF, NDF or LDF files, system failure, malware attack, etc., your SQL Server might stop functioning. In such cases, you can bring back your SQL Server into action by performing database recovery from the MDF and LDF files. In this article, we will show you how to manually restore SQL Server databases from MDF and LDF files using SQL Server Management Studio and T-SQL. Alternatively, you can use the MS SQL Recovery tool to recover SQL databases with just a few clicks without typing command lines manually.

Part 1: Manual Recovery of Database from SQL Server MDF and LDF Files Using UTF-8 Encoding

In SQL Server 2017/2014, there are two ways to restore a database from MDF and LDF files, either by using SQL Server Management Studio or SQL Server itself. Regardless of which method you choose, certain prerequisites must be met.

Notice
The methods below are somewhat technical and, if you're not familiar with SQL Server, it is recommended that you use the easier way to recover databases from MDF and LDF files in SQL Server with the MS SQL Recovery tool from Part 2.
  • If the database is not detached, an error message is displayed.
  • Place the MDF and LDF files in a specific location, such as 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 on the database you need to restore and select "Attach." In the "Attach Databases" window, click the "Add" button.

3. Navigate to the location of the MDF file and select it. Then, click “OK.” SQL Server Management Studio will restore the database from the MDF file.

Method 2: Using T-SQL

Log into your SQL Server database, go to a “New Query Window,” and run the following T-SQL script to attach the MDF file to SQL Server:

CREATE DATABASE dbname ATTACH ( Filename = 'Path_to_your_database_file', Filename = 'Path_to_your_log_file' );

If both of the above methods fail to restore SQL Server database from MDF and NDF files, don't get frustrated. You can still accomplish the task effortlessly with the help of an error-free method discussed below.

Part 2: Using SQL Recovery Tools to Restore SQL Server Database from MDF and LDF Files

When your SQL Server database gets corrupted, it's usually the MDF file that's affected. You can use a professional SQL recovery tool, such as a MDF file repair software, to restore your SQL database. The MS SQL Recovery tool is one such software that allows you to recover databases from MDF files in SQL Server with just a few clicks. Additionally, it automatically repairs damaged log files during the process that might be causing SQL database errors.

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

Step 1: Stop the MS SQL Server service using services.msc or Management Studio.

< strong > Step 2 : Run the SQL Repair Tool. On the main interface, select the MDF / NDF file of the database that you want to recover. Click on 'Repair' to start the repairing process of your MDF / NDF files.

If you know where the file is located, click Browse to locate the database.

If you do not know the file location, click Search to search for the .mdf or .ndf file.

How to Restore Database from MDF File in SQL Server - Step 2 How to Restore Database from MDF File in SQL Server - Step 2

Step 3: Once done, you will see the recovered database objects on the left pane of the window.

How to Restore Database from MDF File in SQL Server - Step 3 How to Restore Database from MDF File in SQL Server - Step 3

Step 4: Click “Export” on the bottom right corner to save your database objects. Choose the desired format, such as MDF or SQL script.

In the Export to Database window, choose Create New Database or Export to Existing Database to save the repaired data. If you choose Create New Database, enter the database name and select the SQL location. If you choose Export to Existing Database, select an existing database.

How to Restore Database from MDF File in SQL Server - Step 4

Step 5 Restart SQL Server.