Using Visual Studio to get the database name from a SQL Server Express database

entity-framework iis-7.5 sql-server sql-server-2008 sql-server-express


I'm currently using this connection string to attach to my database that I created in Visual Studio:

Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database1.mdf;User Instance=true

I'm trying to host the site with IIS so I can mess around with response headers but I'm getting the problem described here: SQL Server Express connection string for Entity Framework Code First

I'm trying to find what database name to specify but not having any luck. I tried Initial Catalog=Database1 but that gave me this error:

Cannot create file 'D:\docs\Visual Studio 2010\Projects\QuickHomePage\QuickHomePage\App_Data\Database1.mdf' because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

I'm just trying to attach to Database1.mdf. Why is it giving errors about trying to create it? One comment suggested attaching the .mdf file to another database instance to see what's inside it.

Would that require running SQL Server Management studio? Every time I try to connect to Server Type Database Engine and the local machine it gives a connection error.

5/23/2017 11:45:17 AM

Accepted Answer

The database name is the name you give your .MDF file as you attach it to the SQL Server (Express) server instance. There is no fixed database name "inside" the MDF that you need to discover - it's totally up to you what you call your database on the server.

So if you attach your Database1.mdf like this:

( FILENAME = N’C:\Data\Database1.mdf’ ),
( FILENAME = N’C:\Data\Database1_Log.ldf’ )

then your database name is CrazyDatabase - but that has no connection whatsoever to the original MDF's file name or any contents inside it - you could call it anything else, too - whatever you choose.

In this case, your new connection string would be:

Server=.\SQLEXPRESS;Database=CrazyDatabase;Integrated Security=SSPI;
9/25/2011 7:07:11 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow