Link to home
Start Free TrialLog in
Avatar of BBRRGG
BBRRGG

asked on

Unable to attach mdf database to SQL Server using Studio Express

When I try to attach a database, receive the following error message.  My PC runs on Windows 7.  Thanks for any help you can provide.

An error occurred when attaching the database(s).

The details are:
TITLE: Microsoft SQL Server Management Studio Express
------------------------------

Attach database failed for Server 'MYNAME-HP\SQLEXPRESS'.  (Microsoft.SqlServer.Express.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.2047.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Attach+database+Server&LinkId=20476

------------------------------
ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.Express.ConnectionInfo)

------------------------------

Unable to open the physical file "C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data\My_DB.mdf". Operating system error 5: "5(Access is denied.)". (Microsoft SQL Server, Error: 5120)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=09.00.5000&EvtSrc=MSSQLServer&EvtID=5120&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Does the file exist there?  If so, does the startup account for the SQL Server service have access to it?
Avatar of BBRRGG
BBRRGG

ASKER

Yes, the mdf file is there.  I believe there should be no access issues.  Other mdf files in the same folder are attaching.

FYI
The last time I had this database open in Studio Express, I renamed the database with a " v2 " in the Object Explorer.  Now, when I go to attach it, I do not see the v2 in any of the mdf file names.
Avatar of BBRRGG

ASKER

FYI
The steps i'm taking are as follows:

Open SQL Server Mgmt Studio Express --> in Object Explorer, right-click on "Databases" --> Attach --> Add --> select my mdf file in the data folder --> OK --> it loads into the "databases to attach" window --> OK (this is when I get the error message above)
What happens when you do this:
EXEC master..xp_fileexist 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data\My_DB.mdf'
Avatar of BBRRGG

ASKER


File exists File is a Directory  Parent Directory Exists
1      0      1
I am afraid I am out of ideas.
On second thoughts, I suspect I know what may be going on.  The database is already attached.
You will also find that since the database is already attached you cannot move, rename or delete it.
Avatar of BBRRGG

ASKER

If its already attached, would it not show up in the object explorer?  I'm not seeing it listed in the object explorer unfortunately.
Does the account the SQL server is running as have modify (not just read) file system permissions on the .mdf file? NETWORK_SERVICE account perhaps?

Might also need modify file system permissions on the file if you are logged into SSMSE using a windows user account instead of a sql server login.
right click on the server properties-> database setting-> the the path to where  your MDf file is present. remember it needs restart to effect the change in the path of SQL Server Database settings
>> I'm not seeing it listed in the object explorer unfortunately. <<
Have you refreshed the Databases collection in Object Explorer?  Or to be certain do you not see it listed in SELECT * FROM sys.master_files?
Failing that is this the only instance of SQL Server on this box?
Have you tried renaming the file?  If so what was the result?  If you can, you can always rename it back.
Avatar of BBRRGG

ASKER

I did refresh and still not seeing it.  When I run SELECT * FROM sys.master_files, I see the other databases but not the ones I've detached.  However, the path in the "Physical name" field is correct, leads to same 'DATA' folder that my desired mdf files are in.

FYI, I left all the default values in the checkboxes when I detached the (I believe 1 was checked and 3 were unchecked).

I believe this is the only instance of SQL server, is there a way I can confirm this?

I tried copying the mdf file within Windows Explorer and then renamed the copied file within Windows Explorer.  When I go to attach this renamed file, it shows up in the SQL Server attach window with the original database name.  And when I click OK to execute the attachment, I receive the same error message.

(changing point value to 500 points)


TITLE: Microsoft SQL Server Management Studio Express
------------------------------

Attach database failed for Server 'MYNAME-HP\SQLEXPRESS'.  (Microsoft.SqlServer.Express.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.2047.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Attach+database+Server&LinkId=20476

------------------------------
ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.Express.ConnectionInfo)

------------------------------

Unable to open the physical file "C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data\My_DB.mdf". Operating system error 5: "5(Access is denied.)". (Microsoft SQL Server, Error: 5120)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=09.00.5000&EvtSrc=MSSQLServer&EvtID=5120&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------
SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BBRRGG

ASKER

Thanks for the feedback.

I appreciate your advice that another database is using this mdf file.  Can you please advise how I can determine which database is using this file, and how I can fix the situation (my goal is to attach the desired database to Studio Express?)  

Currently, the only database attached in Studio Express is the System Database (still getting same error.)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BBRRGG

ASKER

Thanks for the feedback.  The mdf file is not listed in the physical_name column for this query output.  To clarify, when I said above that the path in the "Physical name" field is correct, leading to the correct 'DATA' folder, I was just referring to the path to the folder, not to the mdf file itself.

It seems like it could be a permissions issue, but then again, I did at one point attach a database (though now, after I've detached it, I can no longer attach that same DB... getting the same error).

Can you think of any workarounds?  (ie. create a new DB and somehow copy over the tables?)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BBRRGG

ASKER

Thanks!
Sometime files get corrupted due improper shutdown and hardware failure, so it is recommended that you should regularly check the hardware. After performing these discussed manual method fails then you should try the third party software to easily recover .mdf file.