Applies to: MS SQL Server 2016, 2014, 2012, 2008/2008 R2, and earlier versions

One of the finest database management tools, Microsoft SQL Server is widely used by DBAs, with its SQL Server 2008 and 2008 R2 versions still being quite popular. However, just like any other software, using SQL Server can also lead to certain issues. One such common problem faced by SQL Server users is database corruption, which requires you to recover databases from MDF and LDF files in SQL Server. Fortunately, the method to repair database using MDF and LDF files is quite straightforward, with two different approaches to achieve it. Choose your preferred solution and get your SQL Server database back instantly.

How to restore a database from an MDF file in SQL Server 2008/2008 R2

You have two options to restore your MDF and LDF files to a SQL database.

Method 1: Restore a Database from an MDF File Using SQL Server

You can choose to repair your SQL database using either SQL Server Management Studio or T-SQL.

Preparation:

    • Detach the database that you want to recover.
    • Place the MDF and LDF files in a specific folder at the same location.

Tip 1: Use SQL Server Management Studio

Step 1: Open Microsoft SQL Server Management Studio and navigate to the "Object Explorer."

Step 2: Right-click Databases and select Attach.

Step 3: In the new window, click on “Add.” Search and select the .mdf file of the database that you want to restore. Click on “OK” > “OK.”

Prompt 2: Utilize T-SQL

Run the following command in your terminal:

CREATE DATABASE dbname;

Replace with

filename='Path where you saved the MDF file',
filename='Path where you saved the LDF file'

for appending;

If both of the above methods fail to help you restore SQL Server database from MDF file, don't get frustrated – there's another option you can try.

Method 2: Recover MDF Database from SQL Server Using SQL Recovery Software

A professional SQL repair tool – MS SQL Recovery Tool, can easily meet your needs for database recovery from MDF files in SQL Server. It specializes in restoring damaged databases by repairing MDF files. Additionally, it is adept at:

    • Restore damaged SQL Server database objects – Tables, Triggers, Indexes, Keys, Rules & Stored Procedures
    • Repair database log files that may result in database corruption errors
    • Recover deleted or dropped SQL database records

Now, use the MS SQL Recovery tool to restore your database in SQL Server from MDF files:

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

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 repair of MDF/NDF files.

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

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

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 the scan is complete, you will see the recovered database objects on the left pane of the window.

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

Step 4. Click on “Export” in the bottom right corner of the screen 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 recovered 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 How to Restore Database from MDF File in SQL Server - Step 4

Step 5: Restart the SQL Server now.