How to Open an MDF File in Windows

MDF is an extension shared by two unrelated formats: a Microsoft SQL Server database file and an older CD/DVD disc image format made by other software. Look at what sits next to it. An .ldf file beside it means SQL Server, and you open it by attaching it in SQL Server Management Studio (Databases > Attach > Add). A matching .mds file means a disc image, which Windows has no built-in way to open.

Which kind of MDF file do you have?

Clue SQL Server database file Disc image
Files beside it An .ldf log file, sometimes .ndf files An .mds file with the same name
Where it usually comes from A business app, a developer, a website or a database handed over by IT A game, a software disc or a CD/DVD backup
What opens it SQL Server, after you attach the file The program that created the image

Microsoft’s description of SQL Server database files calls the .mdf the primary data file: it “contains startup information for the database and points to the other files in the database.” The .ldf transaction log “holds information used to recover the database,” and extra data files usually end in .ndf.

Because the .mdf holds the data itself, never delete one that a program still uses. Deleting it removes that database’s contents.

What you need before attaching a database file

Microsoft warns against attaching databases “from unknown or untrusted sources,” since they “could contain malicious code.” If the file arrived from someone you don’t know, stop here and ask where it came from. Then check these points, all from Microsoft’s attach a database page and its CREATE DATABASE reference:

  • A running SQL Server. Microsoft calls SQL Server Express “the entry-level, free database,” and the editions comparison lists a 50 GB maximum relational database size for it. For setup steps, see installing SQL Server Express.
  • SQL Server Management Studio (SSMS). Microsoft’s SSMS install page says SSMS 22 is installed with the Visual Studio Installer.
  • A version at least as new as the file’s. Microsoft: “A database created by a more recent version of SQL Server can’t be attached in earlier versions.”
  • Every data file. All .mdf and .ndf files must be available. A missing log is a separate case, covered below.
  • Folder access. The Database Engine service account must have permission to read the files where you put them, so copy them to a folder that service can read rather than a personal Downloads folder.
  • Your own permission. Attaching requires CREATE DATABASE, CREATE ANY DATABASE or ALTER ANY DATABASE permission. On your own Express install you usually have it; on a work server, ask the administrator.

Attach the database in SQL Server Management Studio

  1. Copy the .mdf file (and its .ldf and any .ndf files) into a folder the SQL Server service can read.
  2. Open SSMS. In Object Explorer, connect to your SQL Server Database Engine instance and expand it.
  3. Right-click Databases and select Attach.
SQL Server Management Studio Object Explorer with Databases right-clicked and the Attach option highlighted
(1) Right-click Databases in Object Explorer, then (2) choose Attach and select the MDF file. (Illustration)
  1. In the Attach Databases dialog box, select Add.
  2. In Locate Database Files, browse to the folder and select the .mdf file.
  3. Look at the database details grid. SSMS lists every data and log file the database expects, and the Message column flags any it can’t find.
  4. Confirm the dialog to start the attach. When it finishes, look for the database under Databases in Object Explorer and expand it to browse its tables.

If the .ldf log file is missing

When a file isn’t where SSMS expects it, the Message column shows “Not found.” Microsoft gives two fixes: point the grid at the file’s real location, or remove the log file from the grid and attach the data file alone.

Removing the log only works in certain cases. Microsoft’s CREATE DATABASE reference says SQL Server rebuilds a missing log automatically for a read/write database with a single log file that “was shut down with no users or open transactions.” The FOR ATTACH_REBUILD_LOG option “automatically creates a new, 1-MB log file,” but it also requires a clean shutdown and every data file.

If the database wasn’t shut down cleanly, ask whoever sent the file for a proper backup instead. Microsoft notes that detaching and attaching “is not recommended” for backup and recovery.

Attach with a T-SQL command instead

If you’d rather type a command, open a new query window in SSMS and use CREATE DATABASE with the FOR ATTACH clause. This is Microsoft’s example; replace the database name and paths with your own:

CREATE DATABASE MyAdventureWorks ON (FILENAME = 'C:\MySQLServer\AdventureWorks_Data.mdf'), (FILENAME = 'C:\MySQLServer\AdventureWorks_Log.ldf') FOR ATTACH;

Leave out the log file line if you don’t have one and the clean-shutdown conditions above apply. Skip older guides that use sp_attach_db: Microsoft says it will be removed in a future version.

Open the file from Visual Studio with LocalDB

Developers can skip the full server. Microsoft describes SQL Server Express LocalDB as “a feature of SQL Server Express targeted to developers.” In SQL Server 2025 and later it’s included with Express; with SQL Server 2022 and earlier it came with Express with Advanced Services. You can also add it through the Visual Studio Installer’s Data storage and processing workload. Its automatic instance is named (localdb)\MSSQLLocalDB.

Following Microsoft’s Visual Studio connection steps:

  1. Open Server Explorer (or SQL Server Object Explorer) and choose to add a connection.
  2. Set the data source to Microsoft SQL Server Database File (SqlClient).
  3. Select Browse next to Database filename and pick the .mdf.
  4. Test the connection, then select OK.

Microsoft adds that LocalDB files “can only be opened by one instance of LocalDB at a time,” so close any other app that has the file open first.

Azure Data Studio is retired: use VS Code or SSMS

Older advice suggests Azure Data Studio. Microsoft’s retirement notice says it “is retired as of February 28, 2026 and no longer receives updates or security fixes,” and points to Visual Studio Code with the MSSQL extension, or SSMS for full administration.

To add the MSSQL extension, open Extensions in VS Code (Ctrl+Shift+X), search for mssql, and install SQL Server (mssql). It connects to a SQL Server; Microsoft’s page doesn’t describe opening a loose .mdf, so attach the file with SSMS or T-SQL first, then connect.

When the MDF is a disc image

Windows can mount some image files on its own, but MDF isn’t one of them. Microsoft’s Mount-DiskImage reference covers ISO files and virtual hard disks, and the Disk Management guide lists .vhd and .vhdx. Microsoft documents no built-in support for MDF disc images.

The reliable route is the source of the image:

  • A game or program you own: get a fresh copy from the publisher’s own store or download page.
  • A backup you or a colleague made: use the program that created the image, or ask the person who made it for the original disc or an ISO copy.
  • A file from an unknown website: don’t open it. Disc images from unknown sources can carry malware.

For a different disc image type with its own options, see how to open a BIN file.

Errors you might see when attaching

  • The file came from a newer SQL Server. It can’t attach to an older version. Install a version at least as new as the one that created it.
  • Access or permission errors. Move the files to a folder the Database Engine service account can read, and make sure your login has one of the permissions listed earlier.
  • The file is bigger than Express allows. Express tops out at 50 GB per database, so a larger one needs another edition.
  • The database is damaged. Ask for a backup rather than trying to repair a copy.

When you’re finished with a database you attached for a quick look, you can remove it again; dropping a SQL database that’s still in use covers the case where SQL Server refuses because a connection is open.

MDF file questions

Can I open an MDF file without installing SQL Server?

Not with Microsoft’s tools. SSMS, VS Code and Visual Studio all read the data through a SQL Server engine. LocalDB is the lightest Microsoft option if you don’t want a full server running.

Can I see the tables in Excel?

Not straight from the file. Attach it to SQL Server first; after that, programs that can connect to a SQL Server database can read it.

Can I rename the database when I attach it?

Yes. The Attach Databases dialog has an Attach As column, and in T-SQL the name after CREATE DATABASE becomes the database name. That helps when a database with the same name already exists.