开发者

Getting the database name from a SQL Server Express database in Visual Studio

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 studi开发者_如何学JAVAo? Every time I try to connect to Server Type Database Engine and the local machine it gives a connection error.


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:

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

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜