There are different reasons that can cause your SQL server to break down. Some of the most common reasons are MDF, NDF, and LDF file corruption. However, you can restore your files in an SQL server using different methods. We will discuss manual ways to recover corrupted/deleted MDF files in SQL server as well as using a powerful third-party data recovery tool.

Part 1. How to Recover Corrupted/Deleted MDF Files?

1.1 Recover Deleted MDF File Using Data Recovery Tool [Recommended]

If you want to recover an MDF file using a third-party data recovery tool, then you can go for iMyFone D-Back. It is capable of recovering lost and deleted data if you know the location of the deleted file.

It supports over 1000 file formats and can recover files under any difficult scenario. It helps you to perform selective data recovery and also offers a file filter to narrow down the search for your required file.

Here is how you can use iMyFone D-Back to recover deleted MDF files.

Step 1: After downloading, install iMyFone D-Back. The tool will present all the connected drives to the computer. Choose the drive from which the MDF was deleted.

interface

Step 2: Scan the drive. iMyFone D-Back will perform a deep scan on the scan to locate deleted and lost files.

scan the selected location

Step 3: It will list all the recoverable files. You can preview the files and select the files you want to recover. After selecting, click on "Recover" to restore the deleted files. You can choose the path where you would like to save these files.

preview

imyfone-dback-hard-drive-recovery-save-files

1.2 Recover Corrupt MDF File Using DBCC CHECKDB Command

Whenever your MDF database is corrupted or deleted, you can use the built-in DBCC CHECKDB command to recover it. It helps to reduce the loss. However, before trying to restore or recover the corrupted or deleted MDF file, make sure to create a backup of your corrupt MDF database. Now, follow the below steps to recover the corrupt MDF file using the DBCC CHECKDB command.

Step 1: Restart the SQL server and do not perform anything.

Step 2: Execute the command below in the SQL Server Management Studio. Assume that the corrupt database is "MyDatabase.mdf".

use master

declare @databasename varchar(255)

set @databasename='MyDatabase.mdf'

exec sp_dboption @databasename, N'single', N'true' -- Set the destination database to single-user mode

dbcc checkdb(@databasename,REPAIR_ALLOW_DATA_LOSS)

dbcc checkdb(@databasename,REPAIR_REBUILD)

exec sp_dboption @databasename, N'single', N'false' -- Set the destination database back to multi-user mode

The command' dbcc checkdb(@databasename,REPAIR_ALLOW_DATA_LOSS)', may lose some of your data in the database while repairing. The command' dbcc checkdb(@databasename,REPAIR_REBUILD)', will not cause any data loss but it takes much time.

Step 3: After the repair operation finishes, you can use the command "dbcc checkdb('MyDatabase.mdf')" to check whether the database is fixed or not.

Part 2. Top 2 Ways to Restore Database MDF and LDF Files in SQL Server

If you want to restore database MDF and LDF files in SQL Server manually, then you have two different methods. It is a little complex. If you have no idea about SQL Server, then it is recommended that you find easier ways to restore the database. However, if you want to use manual methods, there are two prerequisites that you have to follow.

  • Make sure to detach the base; otherwise, you will encounter an error.
  • Secondly, put MDF and LDF files in the same location.

1) On using T-SQL

The first manual method is to use T-SQL.

Step 1: Log in to the SQL Server database.

Step 2: Go to the New Query Window and execute the following command. This will attach the MDF file to SQL Server.

Create database 'dbname'

On

(

Filename= 'path where you copied files',

Filename =' path where you copied log'

)

For attach;

For example,

CREATE DATABASE MyDatabase

ON

(

FILENAME = 'C:\SQLServer\MyDatabase_Data.mdf',

FILENAME = 'C:\SQLServer\ MyDatabase _Log.ldf'

)

FOR ATTACH;

2) Through SQL Server Management Studio

The second manual method to restore database MDF and LDF files in SQL Server is by using SQL Server Management Studio.

Step 1: Open SQL Server Management Studio and head towards the object explorer of the database engine instance.

Step 2: Right-click on the database and select the option of "Attach" from the list.

Step 3: A small window will pop up. Click on "Add". Browse the MDF file and click "OK".

Step 4: Validate the selected MDF file and confirm your selection by clicking "OK". And then, check the database that you attached.

Part 3. FAQs about MDF File Recovery

3.1 What are MDF and LDF files in SQL server?

MDF is Main Database File, and LDF is Log Database File. MDF carries all the information in the database, while the LDF is responsible for keeping a record of all the transactions and changes made to the database. For recovery in a disastrous situation, LDF is extremely important.

3.2 What causes an MDF file to be corrupted in the SQL server?

There are different causes an MDF file can corrupt in SQL Server. A few common causes are:

  • If the MDF file exceeds the storage size limit, then it can corrupt.
  • MDF files can corrupt due to a virus or malware attack.
  • If a network failure occurs during the usage of the SQL database, then it can damage the MDF File.
  • Lastly, if the header of the file is damaged, it will also damage the MDF file.

3.3 How do I open an MDF file?

There are different ways to open an MDF file. It includes automatic and manual ways. To manually open an MDF file, you can launch the SQL Server Management Studio and connect to the SQL Server instance using the "Object Explorer," you can attach the MDF file. Find the MDF file in the location and click on "OK".

3.4 How to delete MDF and LDF files?

Here is how you can delete the MDF or LDF files from the SQL database.

Step 1: Go to "Object Explorer" and connect to the SQL Server Database Engine.

Step 2: After expanding the instance, expand the option of "Databases" and right-click on the database from which you want to delete the file. Click on "Properties".

Step 3: Choose the Files page. In the grid of the database, choose the file that you want to delete, and click on "Remove," then click on "OK".

Conclusion

In this article, we have discussed MDF and LDF files along with SQL Server. There are different ways in which you can recover deleted MDF and LDF Files in the SQL Server. However, the best method is to go for the iMyFone D-Back. It is an advanced tool that can recover any type of deleted data. It has a user-friendly interface and a trial version that users can download for free and try this tool.