How to Fix SQL Server Error 5172 — MDF File Header is Not a Database
SQL Server Error 5172 is a file-level error that stops SQL Server from even beginning the database attach or recovery process. The full message reads: "The header for file '[filename]' is not a valid database file header. The %ls property is incorrect." This error means SQL Server looked at the first page of your MDF file — the page that contains fundamental database metadata — and could not recognize it as a valid SQL Server database structure.
Error 5172 is particularly alarming because it suggests the problem is at the very root of your data file. Unlike errors that affect individual data pages, a corrupt file header means SQL Server cannot determine the database name, version, collation, file group layout, or any other foundational property. Without this information, it refuses to proceed, leaving the database completely inaccessible.
MDF Header Corrupt? Recover Data Directly
Data Repair Pro is purpose-built for exactly this scenario. It parses MDF files at the binary level — reading past a damaged or missing file header — to directly access the data pages that store your actual records. Preview your tables and export data even when SQL Server reports Error 5172.
Download Free Data RepairWhat Is the MDF File Header?
Every SQL Server MDF (primary database file) begins with a special structure stored in Page 0 called the File Header Page. This page is 8 KB in size and contains critical metadata including:
- The SQL Server version that created the database (the CompatibilityLevel)
- The database GUID and internal identifiers
- The file's sequence number and file group membership
- Collation settings that govern string comparison and sorting
- Checksum values that verify page integrity
- The internal log sequence number (LSN) marking the last committed transaction
If this 8 KB page is damaged, overwritten, or contains unrecognized values, SQL Server immediately raises Error 5172 and refuses to proceed. There is no workaround within SQL Server itself — it will not guess or ignore a corrupt header.
Common Causes of SQL Server Error 5172
- File system corruption: A failed write operation caused by a power outage, OS crash, or disk error corrupted the first few kilobytes of the MDF file — the exact location of the file header page.
- Wrong file renamed to .mdf: A non-SQL Server file (e.g., a text file, an Access database, or a ZIP archive) was accidentally renamed with an .mdf extension and is being attached to SQL Server.
- Partial write during detach or copy: The database was copied or moved while still in use, or a file copy was interrupted mid-transfer, resulting in a truncated or zero-filled header region.
- SQL Server version incompatibility: A database file from a significantly newer version of SQL Server (e.g., SQL Server 2022) is being attached to an older instance (e.g., SQL Server 2016) that cannot understand the newer file format.
- Bad sectors at the start of the disk: Physical media failure at the beginning of the physical disk block where Page 0 is stored makes the header unreadable, even though the rest of the database data may be intact.
- Overwritten by another application: Another program opened the MDF file path and wrote non-SQL data to it, destroying the header signature that SQL Server uses to recognize it as a database.
Step-by-Step: How to Fix SQL Server Error 5172
Step 1 — Confirm You Have the Right File
Before doing anything else, verify the file you are trying to attach is actually a SQL Server MDF file. A genuine MDF file begins with a specific binary signature. Open a PowerShell terminal and run the following to inspect the file header bytes:
# Read first 16 bytes of the MDF file as hex
$bytes = [System.IO.File]::ReadAllBytes("C:\Data\YourDatabase.mdf")
$bytes[0..15] | ForEach-Object { $_.ToString("X2") } | Join-String -Separator " "
A valid SQL Server MDF file will begin with specific SQL page signature bytes. If the file starts with "50 4B" (ZIP), "D0 CF" (old Office format), or all zeros, the file is not a valid MDF.
Step 2 — Check the SQL Server Version Compatibility
SQL Server cannot attach databases from newer versions to older instances. Check the version of the SQL Server that originally created the MDF and compare it to the instance you are trying to attach it to.
-- Check current SQL Server version
SELECT @@VERSION;
SELECT SERVERPROPERTY('ProductVersion');
If the MDF was created on SQL Server 2019 (internal version 904) and you are attaching to SQL Server 2017 (internal version 869), SQL Server 2017 cannot read the newer file format and raises Error 5172. The solution is to attach the file to a SQL Server instance of the same or newer version.
Step 3 — Check File Size and Integrity
A legitimate MDF file must be a multiple of 8 KB in size (the SQL Server page size). If the file size is not divisible by 8192 bytes, the file was truncated or corrupted during a transfer. Also run chkdsk on the drive to detect and fix filesystem-level errors that may be misreporting the file contents.
# Check file size in PowerShell
(Get-Item "C:\Data\YourDatabase.mdf").Length
# A valid MDF size modulo 8192 should be 0
(Get-Item "C:\Data\YourDatabase.mdf").Length % 8192
Step 4 — Try Attaching with CREATE DATABASE FOR ATTACH
If the header is only partially damaged, the FOR ATTACH_REBUILD_LOG option may bypass some header validation issues and allow SQL Server to reconstruct the log while attaching.
-- Attempt to attach with log rebuild
CREATE DATABASE YourDatabaseName
ON (FILENAME = 'C:\Data\YourDatabase.mdf')
FOR ATTACH_REBUILD_LOG;
GO
Step 5 — Restore from a Verified Backup
If the MDF file header is genuinely corrupt and the data inside is intact, a restore from a backup taken before the corruption occurred is the most reliable fix. Use RESTORE DATABASE ... FROM DISK with the WITH REPLACE option to overwrite the damaged file with a clean, known-good copy.
When Standard Fixes Fail: Use Data Repair Pro
If none of the above steps resolve Error 5172, the file header is likely severely damaged and SQL Server will never accept it. This is exactly the scenario Data Repair Pro was designed for. Instead of going through SQL Server's attach process — which requires a valid header — Data Repair Pro reads the MDF at the raw binary level, locating and parsing data pages directly by scanning the file's internal page structure.
This means even if Page 0 (the file header) is completely destroyed, Data Repair Pro can still find and recover the system catalog pages, user table pages, index pages, and BLOB data stored throughout the rest of the file. You get a full list of recoverable tables and row counts before you commit to any repair — zero risk, maximum recovery.
Your MDF Header Is Corrupt — We Can Still Recover Your Data
Data Repair Pro scans your MDF file at the binary page level, recovering tables, rows, and schemas even when the file header is completely unreadable by SQL Server.
✓ Free preview of all recoverable data ✓ No SQL Server required ✓ Works on all SQL Server versions
Download Free — Windows