Attaching an MDF Database to LocalDb in Visual Studio the Easier Way

I’ve been loving Visual Studio 2012’s SQL Server Object Explorer (SSOE) instead of always jumping out to SQL Server Management Studio (SSMS) to do database management. SSOE covers many (not all) common tasks…and many more than VS’s Server Explorer let’s you do.

But I spent an embarrassing amount of time trying to figure out how to easily connect an MDF file to my SQL Server 2012 LocalDB instance because, duh …I was doing it wrong! Over and over and over again.

SSOE lets you inspect databases that are already attached to localdb and it lets you create new localdb databases. I couldn’t figure out how to attach to an existing database.

Finally, some brain cells returned to my skull and I went over to VS’s Server Explorer and attached the database there (and provided a nice logical name rather than ending up with the rangy file-path-as-database-name).

Once it was attached to localdb, then I could see it in SSOE and actually access it in my app using a normal connection string.

Just “duh” for how much time I spent on this.

It was important to me because I need to distribute sample solutions along with existing databases and I want the database files in the solution folder and *simple* steps I could relay to users to make sure they could use them when they want to debug and test out the solution themselves. I’m perennially looking for a simpler way to do this. I’ve used SQL Express, SQL CE, database scripts, packaged exes, you name it. And I always spend hours trying to do it in a way that it’s really simple for the dev who is trying out my sample solution.

Oh and one more trick for you about localdb

I have always had a problem remembering forward slash vs. back slash. Here’s a blog post from 2006 as evidence! So connecting to localdb is always a challenge because you specify it as (localdb)\v11.0.

My little secret to get the back slash correct is visual:

image

I know it’s totally dorky, but I just make sure that the slash is parallel to the first part of the “v”.  Hey, it works for me, what can I say? :)

Shout it
#1 Jason Haley on 1.27.2013 at 8:08 AM

What if you forget which part of the v it is parallel with? Maybe it was the second part, not the first part?

;)

#2 Julie on 1.27.2013 at 1:05 PM

When that time comes, it might be a sign for a new lifestyle. :)

#3 Paul on 1.28.2013 at 1:39 PM

Phew! Been programming nearly 20 years and thought it was only me that struggled with slashes ;-)

#4 Sam Sitienei on 1.31.2013 at 2:01 AM

Hey Julie your material are very importance. I followed MVC 3 and acquired lots. Kenyan

#5 shipra on 2.01.2013 at 10:20 AM

I am not a programmer but I have this SQL subject this session and have to prepare for it. What all topics should be covered in it?

And has anyone studied from this course www.wiziq.com/course/125-comprehensive-introduction-to-sql of SQL tutorial online?? or tell me any other guidance...

would really appreciate help

#6 @rafapefgmail on 3.20.2013 at 7:07 AM

Great tips, I had the same issue. =)

Leave a Comment