Step 1: Verify what files you have
You should have one or both of these:
-
YourDatabase.mdf→ the primary data file (required) -
YourDatabase_log.ldf→ the transaction log file (optional; SQL Server can recreate it if missing)
Step 2: Copy files to the SQL Server machine
Move the .mdf (and .ldf if available) to a safe folder on the same machine where SQL Server is installed, such as:
C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA
Step 3: Attach the database using SSMS
- Open SQL Server Management Studio (SSMS).
- Connect to your SQL Server instance.
- In Object Explorer, right-click Databases → Attach...
- Click Add..., browse to the
.mdffile.- SSMS will automatically detect the
.ldffile if it’s in the same folder.
- SSMS will automatically detect the
- Click OK.
The database will appear under Databases in SSMS.
You can do the same via a query window:
CREATE DATABASE YourDatabase
ON (FILENAME = 'C:\Data\YourDatabase.mdf'),
(FILENAME = 'C:\Data\YourDatabase_log.ldf')
FOR ATTACH;
If you don’t have the log file, you can tell SQL Server to rebuild it:
CREATE DATABASE YourDatabase
ON (FILENAME = 'C:\Data\YourDatabase.mdf')
FOR ATTACH_REBUILD_LOG;
If you only have a .bak file instead of .mdf, the process is different — you'd restore it instead of attaching.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin