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.
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.
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.
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.
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.
Step 3: Once done, you will see the recovered database objects on the left pane of the window.
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.
Step 5 Restart SQL Server.