Let’s say someone wants to give you their SQL Server database. Maybe they want you to manage it, troubleshoot it, or learn from it. So they hand you a .MDF file and .LDF file and tell you that you need SQL server 2008 R2 to open them. So you install SQL Server 2008 R2 Express (which, to your relief, is free). You open SQL Server Management Studio(SSMS), and you try to open the .MDF file. You get the following error:

There is no editor available for 'C:\Temp\MyDatabase.mdf'. Make sure the application for file type (.mdf) is installed.
The Solution:
.MDF files are SQL Server database files and .LDF files are the associated log files. But you can’t really “open” them. Instead, you have to “attach” to them. Once you attach, you’ll see the database in the object explorer of SSMS.
If you’re thinking, “Why wasn’t the Backup and Restore process used instead of passing around these database files?” It’s a valid question, but it doesn’t help the reader staring at .MDF and .LDF files with no clue how to access them. However, if you need to move a database around, you should do a search on “backup restore attach detach sql server” (without the quotes) in your favorite search engine. You’ll get lots of opinions to help you decide the best approach. |
How to Attach in a Perfect World:
There’s a couple of different methods. I’ll give you the “easy” way through SQL Server Management Studio (SSMS) and then point you to other methods using T-SQL queries.
- Launch SSMS.
- Connect to your SQL Server Instance.
- Right-click on Databases in the Object Explorer.
- Click Attach.
- In the Attach Databases window, click the Add button.
- Navigate to the directory containing the .MDF and .LDF files.
- Select the .MDF file, and press OK.
- Press OK again to attach the database.
You should see the database appear in the Databases node (press F5 if you don’t). You can now explore the data using SSMS. These same steps are documented in the MSDN here. I’m just summarizing to make it easier for you.
There are two ways to accomplish the same thing in a T-SQL script. First, you can use the sp_attach_db stored procedure. This is shown here. Or you can use the CREATE DATABASE command with the FOR ATTACH argument.
So, the World isn’t Perfect
If your database is successfully attached, then stop reading and go use it. But if you see any errors or unexpected behaviors, I’ll try to share some troubleshooting suggestions.
First, you may get a generic error like the following..

An error occurred when attaching the database(s). Click the hyperlink in the Message column for details.
You might be asking, “Where’s the hyperlink?” I’ll show you in the next screenshot (circled in red), and it will also be our first example.
1. Access Denied:

- Attach database failed for Server ###. (Microsoft.SqlServer.Smo) Unable to open the physical file “####”. Operating system error 5: “5(Access is denied.)”. (Microsoft SQL Server, Error 5120)
This access denied error could have a few different causes. Basically, you don’t have the required access to the .MDF or .LDF files. This can happen if you get the file from someone else. When they detach the database file, the file permissions are changed to give only that user full control. Also, even if you see that the file has full control for the Administrators group, it may not be enough. Remember in Windows 7/Vista, those permissions are often only realized when you’re running an application as an Administrator.
So here are several choices to fix it:
- The easiest solution is to close SSMS and then run it as an Administrator. Perform the attach as an Administrator, and it’s likely going to work.
- Another solution is to explicitly grant full control to the .MDF and .LDF files to your user account. This can be done by right-clicking the files, selecting Properties, and modifying the Security tab.
- A final solution is to copy the files to the default directory for your other database files. To find out what that is, you can use the sp_helpfile procedure in SSMS. On my machine it is: C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA. By copying the files to this directory, they automatically get permissions applied that will allow the attach to succeed.
2. Access Denied Variation 2
There is another variation of the Access Denied message that has a simple solution:

Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc) CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file '####'. (Microsoft SQL Server, Error: 5123)
This error means that the file is already opened exclusively by another application. The most likely cause is that this database file is already attached to an instance of SQL Server. Double-check your list of databases to see whether it is already in the list. It’s also possible for applications to use .MDF files directly with a feature called User Instances. If an application is using a .MDF file in this way, then it would have to close before you could attach the to that database file.
3. Database is Read-only
This is not so much an error as an undesired result. When you load the database, it is in a read-only state. You can tell by the gray shading of the databasee icon (not to mention the “Read-Only” label next to it).

The most probable cause is that the .MDF file you attached to is read-only. Just right-click on the .MDF and .LDF files, select properties, and then uncheck the Read-only check box.

4. Unable to Downgrade
The next error happens when you try to attach a database of a higher version of SQL Server (like SQL Server 2008 R2) to a SQL Server Instance of a lower version (like SQL Server 2005).

Attach database failed for Server '####'. (Microsoft.SqlServer.Smo) An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo) The database '####' cannot be opened because it is version 661. This server supports version 655 and earlier. A downgrade path is not supported. Could not open a new database '####'. CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 948)
This has never been supported. You can attach a database of an older version of SQL Server to a newer version, but you can’t go the other direction. If you want to verify the version of SQL Server you’re running, see this post.
A Parting Word on Detach:
This post is just covering the scenarios of, “How do I open this .MDF file” and “Oh, I have to attach it, why am I getting errors?”. But how were these files obtained in the first place? The answer is that there is a corresponding Detach feature in SQL Server. You can find it by right-clicking on the database in SSMS, selecting tasks, and looking for Detach. Before you do this, though, run the sp_helpfile procedure in a query window in the context of your database. This will tell you *where* the detached .MDF file will be located. After you’ve detached the file, you can attach it to any SQL Server instance. Although this post was about Attach, I think it’s worth mentioning this for completeness.

If you’re thinking, “Why wasn’t the 


Thanks… it is very useful for me…
And i can do me work again…
i have installed sql server 2005, when i open a .mdf file it says,
“make sure that the application file type(.mdf ) is installed”
wat to do? plz help
and whenrver i add a .mdf file it says
downgrade path is not supported
Are you using SQL Server Management Studio? If the .MDF file was created in SQL Server 2008, for example, then you can’t attach it in SQL Server 2005. You might be running into that problem. You could try uninstalling 2005 and installing 2008 instead. Here is a video that shows how to get the latest version of SQL, and then just follow the steps to attach. How to Install SQL Server 2008 R2 Express.
This post is just covering the scenarios of, “How do I open this .MDF file” and “Oh, I have to attach it, why am I getting errors?”.
i will attach a new database in.mdf file its show error is microsoft sql server Error 5171 how is solve the error message
sir
your advise are good and very informative
the issue m facing is that
i made a certain tables in sql 2005 bt after that my window got currpted and i eventully had to reinstall my window and i lost my previous data in sql…….
bt what i have is a database folder that consisted of those table ,the folder was used in an asp.net application…..
now i have reinstall 2005 sql
sir kindly tell me how can i get my previous sql 2005 tables again…….
and i have read ur post i m also getting the errors on attaching the mdf file……..
the error says
“TITLE: Microsoft SQL Server Management Studio Express
——————————
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.Express.ConnectionInfo)
——————————
ADDITIONAL INFORMATION:
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).
Could not load the DLL xpstar90.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.). (Microsoft SQL Server, Error: 17750)
sir kindly help me in reconnecting to my old 2005 sql tables……..
i shall be waiting for ur informative reply…………
Had the Same problem and followed your Solution, You are Awesome
Thanks,
Now I can go back to goofing off.
FxM
thanku sooooo muchh
I have a mdf and ldf file created in visual studio 2005 with SQL server 2000 now i want to open that files in visual studio 2008 with SQL server 2005 is it possible to use that files in SQL server 2005 ???? Plz help me on this i will be very thankful to u plz
I have not tried going from SQL Server 2000, but it should work according to http://msdn.microsoft.com/en-us/library/ms189625.aspx.
how can i find .mdf file in my sqlserver………….
.mdf file presented in your programs file……..
Sir thanxx for this generous help …
Perfect solution..! Thank you so much. This post was a big help for me.
thanks its rally helpful………………………..
You save my life! (my programmer life of course), spend hours and hours under pressure from my boss until I found your link, thank you!!
Very needful
thanks
Hi, previously, I was able to attach the .mdf file in SSMS 2008 succesfully, but after I closed the program and wanted to open the database again, it is said that error 3415 occured. What do I have to do to fix the problem? Thank you for your help
This helped me a lot..Thanks
Great job. Thanks
Thanks You….
I accidentally changed the properties of the .mdf file to open with notepad.. i tried all your methods to open the .mdf database yet its of no use. Any suggestions? I keep getting the error message of the first type you mentioned…
it’s really help me, thank you
thank you very much, updated to win8 consumer preview & it isn’t supported @ by my support desk at school (don’t know why…) really helped thanks!!!
Brilliant!
hello all I have a problem with ldf log file I need to maximize the the size of it how??? can anyone help me please??
Thanks Jason…your first given suggestion is working fine.
Hello,
I have a little problem… I use (Windows XP) not (Windows 7 or Vista)… how to grant full control permission in Windows xp?
Somebody please help me also can send answer to Syid98@gmail.com
tanks
Hi how are you, Mr. Jason.
I am not on Mdf file. Figure 4 is pretty difficult.
Please help if you can.
Another problem I have is that I do not speak much English.
I’m from Iran.
Thanks for everything
Thanks Sir, it is very helpfull to me………………
i have installed sql server 2005, when i open a .mdf file it says,
“make sure that the application file type(.mdf ) is installed”
wat to do? plz help……….it is created also in 2005……..
When trying to attach a new .mdf I get an error. The file is compressed…It must be decompressed
I’m using SSMS 2008 R2. Any ideas how to fix this issue?
Here is the link with SQL server Management studio it may help any one
http://www.reddyinfosoft.blogspot.in/2012/06/attach-mdf-file-to-sql-server-using-sql.html
Thanks, useful, useful post!
thanks for reply…………..
CREATE DATABASE name_of_database_which_u_want
ON (FILENAME = ‘E:\BBDB1.mdf’),
(FILENAME = ‘E:\BBDB1_log.ldf’)
FOR ATTACH;
========================================
this this click on execute if above methods are failed as in my case
this will defineltly work i see it on sql site
“Process cannot open the file because it is in use by another process”
How do I find which application is using the file and disable it?
I closed down my business but neglected to export MY DATA, accumulated over 5 years, to Excel or some other generic format. The vendor wants me to re-license the software if I want to open the application. Their shortest term is 12 months! I am not in the business any longer and I have no use for the application, I merely want to get MY DATA.
My software licensor says that someone “with a knowledge of MS SQL” can open the files. I am a novice to SSMS but I’ve managed to get as far as attaching the file to create a new database but then I get the above error message.
So, do you have the database in your list? Or when you try to attach the database, you get the error that another application is using the file? If you can’t attach the database because it is in-use, then you could try to figure it out with a tool called “Process Explorer” from the TechNet.microsoft.com website. You can do a search to find it.
I created database in Sql server2008 but i am nont able to attach that one in Sql server2008r2
I could not resist commenting. Very well
written!
thanks …i load file from different location and gave full access to mdf file by right clicking….thnx
Hi Jason,
Thanks for your nice explanation of opening a MDF file. Here is one of the scenarios I ran into as you mentioned ( when I try to attach in SSMS) , expect I error no it little different.
CREATE FILE encountered operating system error 32(The process cannot access the file because it is being used by another process.) while attempting to open or create the physical file ‘D:\SID_DNN\App_Data\Database.mdf’. (.Net SqlClient Data Provider)
This is SS 2008-R2 version. I was “Attaching Database” as Server\Administrator. I need to view the database schema of this database and query few tables to see how the data is stored to be enable me to do reporting. This database this accessed by a live DNN module in our website. How do you suggest I view the schema and query the tables in this databases ? Thanks for your help in advance.
Adding to above question, can I explore the database in Read-Only mode in SSMS since it is being used by another process. thanks!
From Jason suggestions to stop the process accessing the .MDF and then “Attach” to SSMS. – Once “Attached” I should be able to view/explore the db – correct?
But then , if we can resume the DNN process to access the db as before – will I run into any hurdles. In other words can one explore the database in SSMS while the database is in use by an application?
This was great, works like a charm, Many thanks
Very helpful tutorial… Work perfectly… Thank you very much… regards from Indonesia
thanks..
I received the following error with the attach “Directory lookup for the file “d:\data\MSSQL\data\Metric_info_log.ldf” failed with the operating system error 21 (the device is not ready). (Microsoft WQL Server, Erorr; 5133)
How do i correc this? I gave all users full access and also access SQL Server studio in an Administrator role. I don’t think the error received pertains to that of a permission error. I’ve also added the .mdf file to the SQL server data path as mentioned as well… Nothing.
Any info that anyone has to assist in solving this issue would be great.
Thanks in advance,
LJ
Running SSMS as administrator and changing .mdf file permissions to full has really helped me.
Cheers
Thank u Sir.
It is very usefull information that solve my all problems.
Hello Roth. Your post very useful. Thanks! This is very helpful information in this post.
I’ve recived another error and cannot understand what problem is. Can you help me? I’m absolutely new in SQL Server.
I’ve insatlled SQL Server 2012 Express. My operating system is Windows Vista 32-bit. In addition, Microsoft SQL Server Management Studio was run with administrator’s permission.
I tried to attach the next database – PUBS.MDF. This database is stored here http://www.microsoft.com/en-us/download/details.aspx?id=23654 . But, I recieved the next message when I tried to attach database:
Attach database failed for Server ‘GAMBLER\ANTONSQLSERVER’. (Microsoft.Sql.Server.Smo)
Additional information:
- An exception occured while executing a Transact-SQL statement or batch. (Microsoft.Sql.Server.ConnectionInfo)
- Database ‘pubs’ cannot be upgraded because its non-release version (539) is not supported by this version of SQL Server. You cannot open a database that is incompatible with this version of sqlservr.exe. You must re-create the database. Could not open new database ‘pubs’. CREATE DATABASE is aborted. Microsoft SQL Server, Error: 950).
And what does “non-release” version mean? I also tried to understand, why my version os sqlservr.exe is incimpatible, but I can’t understand. The ‘Pubs’ database was created on SQL Server 2000, but my version more new. What’s problem?
thanks, it worked perfectly.
Superb. Thanking you very much. It really helped.
Thank you.. It helped me
SQL User`s if any case your SQL server database is corrupted and damaged then don’t , try SQL Recovery Software which is finest software for recovery of Corrupted SQL server MDF file. For more info then read this article: http://www.articlesbase.com/software-articles/sql-database-recovery-assured-with-technical-intellect-application-6538100.html
Thank you very much sir . it helpled me and now am able to attach mdf.
Thank You for providing helpful info.
Sir,
Thank you for sharing a information with us. I am a student and i am preparing my collage project. I had created one web application which also have database. Now problem is when i am trying to attach my MDF and LDF file in a hosting server its giving me an error “access denied”.
could you please give me some advise why this error is appearing.
You can open ldf files with http://www.apexsql.com/sql_tools_log.aspx