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

Question

I'm presently attaching to my Visual Studio-created database using the following connection string:

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

To play around with response headers, I'm attempting to host the website using IIS, but I'm encountering the issue stated here: Entity Framework Code First connection string for SQL Server Express

I'm having trouble figuring out which database name to use. A tryInitial Catalog=Database1 but I got the following 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.

Just attempting to connect toDatabase1.mdf . Why does it keep throwing errors when I attempt to construct it? One commenter advised seeing the contents of the.mdf file by connecting it to a different database instance.

Would you have to launch SQL Server Management Studio for that? Every time I attempt to connect to the local computer and Server Type Database Engine, a connection error is returned.

1
2
5/23/2017 11:45:17 AM

Accepted Answer

The name you provide your.MDF file when you connect it to the SQL Server (Express) server instance is the database name. The name you give your database on the server is entirely up to you; there is no predetermined database name "within" the MDF that you need to find.

Then, if you link yourDatabase1.mdf as in this:

CREATE DATABASE CrazyDatabase ON
( FILENAME = N’C:\Data\Database1.mdf’ ),
( FILENAME = N’C:\Data\Database1_Log.ldf’ )
FOR ATTACH

the name of your database isCrazyDatabase - but it has nothing to do with the name of the original MDF file or any of the data inside of it. You may call it anything you like.

Your new connection string in this situation would be:

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


Related Questions





Related

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