从Visual Studio中的SQL Server Express数据库获取数据库名称 [英] Getting the database name from a SQL Server Express database in Visual Studio

查看:254
本文介绍了从Visual Studio中的SQL Server Express数据库获取数据库名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此连接字符串附加到我在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

我正在尝试使用IIS托管该网站,所以我可以弄乱响应头,但我得到这里描述的问题:实体框架代码的SQL Server Express连接字符串首先

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

我正在尝试找到要指定的数据库名称,但没有运气。我试过初始目录= Database1 ,但这给了我这个错误:

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:


无法创建文件'D:\docs\Visual Studio
2010 \Projects\QuickHomePage\QuickHomePage\App_Data\Database1.mdf'
因为它已经存在。更改文件路径或文件名,
重试操作。

CREATE DATABASE失败。列出
的某些文件名无法创建。检查相关错误。

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.

我只是试图附加到 Database1.mdf 。为什么会出现创建错误?一个注释建议将.mdf文件附加到另一个数据库实例,以查看其中的内容。

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.

需要运行SQL Server Management studio吗?每次尝试连接到服务器类型数据库引擎和本地机器时,都会发生连接错误。

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.

推荐答案

数据库名称为您将.MDF文件附加到SQL Server(Express)服务器实例时的名称。您需要发现的MDF内部没有固定的数据库名称 - 完全取决于您在服务器上称为数据库。

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.

所以如果你附加您的 Database1.mdf 如下所示:

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

那么你的数据库名称是 CrazyDatabase - 但没有连接到原始MDF的文件名或其中的任何内容 - 您也可以调用其他任何东西 - 无论您选择什么。

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;

这篇关于从Visual Studio中的SQL Server Express数据库获取数据库名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆