David Mullin
David Mullin

Reputation: 688

SqlExpress LocalDb can't open file

I have two MDF files in a directory down the AppData/Local path. If I attempt to open them using LocalDb.

My connection string is of the form:

Data Source=(localdb)\v11.0;AttachDbFilename="C:\Users\Anna\AppData\Local\CaseTrakker Software\CTDynamoDisconnected\CTDynamoDisconnected_Data.mdf";Integrated Security=True;Connect Timeout=10

I have a sample desktop application that attempts to connect to this MDF, and I get this exception:

System.Data.SqlClient.SqlException (0x80131904): Cannot open database "C:\USERS\ANNA\APPDATA\LOCAL\CASETRAKKER SOFTWARE\CTDYNAMODISCONNECTED\CTDYNAMODISCONNECTED_DATA.MDF" requested by the login. The login failed. Login failed for user 'IMA\Anna'.

If I move this file to any other location, or rename it (even to a name that is longer), I am able to connect to it.

There appears to be something peculiar about this location or something.

One other odd thing: it worked last week. So far as I am aware, nothing has changed on my machine or my Domain Security.

I'm at a complete loss as to what else to even try. Ideas?

Upvotes: 2

Views: 6639

Answers (3)

Korayem
Korayem

Reputation: 12497

In my case I had that DB for a while and mistakenly deleted its MDF and LDF files.

To solve this, I opened SQL Management Studio and connected to (localdb)\MSSQLLocalDB using Windows Authentication then created manually a new empty DB with desired name like that in web.config connection string

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=myDb;Integrated Security=True;" providerName="System.Data.SqlClient" />

Upvotes: 0

Jaxidian
Jaxidian

Reputation: 13511

One cause of this problem is if you go into your C:\Users\[username] folder and delete the MDF and LDF files. If you do this, then that's akin to doing the same thing to full-blown SQL Server. The server instance still thinks it has the databases but they're obviously not going to work.

A work-around to the problem is to change the database name in your connection string and it should just work.

To actually fix the problem, open up SQL Management Studio, connect to server (LocalDb)\v11.0 (likely with Windows Authentication) and you can detach these databases this way.

Upvotes: 1

Krzysztof Kozielczyk
Krzysztof Kozielczyk

Reputation: 5937

Can you check if there is anything interesting in the LocalDB instance log file? It is located by default in %localappdata%\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0 folder.

Upvotes: 2

Related Questions