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.
You have two options to restore your MDF and LDF files to a SQL database.
You can choose to repair your SQL database using either SQL Server Management Studio or T-SQL.
Preparation:
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.”
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.
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:
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.
Step 3: Once the scan is complete, you will see the recovered database objects on the left pane of the window.
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.
Step 5: Restart the SQL Server now.