I have an error below adding an .mdf
file (SQL Server Database) in a Visual Studio 2010 project
在Visual Studio 2010项目中添加.mdf文件(SQL Server数据库)下面有一个错误
Connections to SQL Server database files (.mdf) require SQL Server 2005 Express or SQL Server 2008 Express to be installed and running on the local computer
连接到SQL Server数据库文件(.mdf)需要在本地计算机上安装和运行SQL Server 2005 Express或SQL Server 2008 Express
I don't want to install SQL Server Express (2005/2008) because I have already installed SQL Server 2005 Enterprise Edition
我不想安装SQL Server Express(2005/2008),因为我已经安装了SQL Server 2005 Enterprise Edition。
I am using Visual Studio 2010 Ultimate
我正在使用Visual Studio 2010 Ultimate
3 个解决方案
#1
10
This is a really annoying one. Basically, in Machine.config for the version of the framework you are developing against, there is an entry for LocalSqlServer.
这真的很烦人。基本上,在机器。对于正在开发的框架的版本,有一个LocalSqlServer条目。
On my machine, for version 4:
在我的机器上,版本4:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\Machine.config
C:\Windows\ Microsoft.NET \ Framework \ v4.0.30319 \ Config \ Machine.config
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
I found that if I changed the data source part of the connection string to point at my Sql 2005 full server instance, then the error you mentioned went away.
我发现,如果我将连接字符串的数据源部分更改为指向Sql 2005的完整服务器实例,那么您提到的错误就会消失。
(Similar for other versions of the framework, which I also changed)
(类似于框架的其他版本,我也对其进行了修改)
I can't remember if I needed to restart just visual studio or the whole machine before I saw the changes work.
我不记得在看到更改之前是否需要重新启动visual studio或整个机器。
Remember to back up your machine.config files before editing them!
记得备份机器。配置文件之前,编辑他们!
With that being said, there's also no reason why you can't add the database into Sql Server itself (if you have the mdf) then connect to it from Visual Studio via the View -> Server Explorer -> Data Connections (Right Click -> Add Connection) - have you tried that?
既然如此,也没有理由不能将数据库添加到Sql Server本身(如果您有mdf),然后通过视图->服务器资源管理器->数据连接(右键单击->添加连接)从Visual Studio中连接到它,您试过了吗?
#2
1
I know this post is a bit old but i encountered the same problem and i actually found a solution, so i would like to share it.
我知道这篇文章有点过时,但我遇到了同样的问题,我找到了一个解决方案,所以我想和大家分享一下。
- Install sql express 2008 r2
- 安装sql express 2008 r2
- In visual studio 2010 go to
Tools -> Options
- 在visual studio 2010中使用工具->选项。
- Select
Database Tools -> Data Connections
and update theSql Server Instance Name (blank for default)
with the instance name of your database. - 选择Database Tools ->数据连接并使用数据库的实例名更新Sql Server实例名(默认为空白)。
- Then go to services by pressing ⊞Win + R and
services.msc
- 然后去服务按⊞+ R和services.msc获胜
- Select the
SQL Server (<instance name of express edition>)
, right click and selectProperties
- 选择SQL Server (< express edition>的实例名),右键单击并选择Properties
- Then in the properties window of the service go to
Log On
tab and selectLocal System account
- 然后,在服务的属性窗口中,转到Log On选项卡并选择Local System account
After these steps i was able to add a .mdf
file into visual studio 2010.
在这些步骤之后,我能够将.mdf文件添加到visual studio 2010中。
Also maybe is possible to be able to do it without installing Sql server express, just starting from the second step, but i did not try it.
也许还可以在不安装Sql server express的情况下做到这一点,只是从第二步开始,但我没有尝试。
#3
0
you can use code to add that if not exist
如果不存在,可以使用代码添加
string curFile = @"C:\Dev\Test_data.mdf";
if (!File.Exists(curFile))
{
SqlConnection connection = new SqlConnection(@"server=(localdb)\v11.0");
using (connection)
{
connection.Open();
string sql = string.Format(@"
CREATE DATABASE
[Test]
ON PRIMARY (
NAME=Test_data,
FILENAME = '{0}\Test_data.mdf'
)
LOG ON (
NAME=Test_log,
FILENAME = '{0}\Test_log.ldf'
)",
@"C:\Dev"
);
SqlCommand command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
}
}
#1
10
This is a really annoying one. Basically, in Machine.config for the version of the framework you are developing against, there is an entry for LocalSqlServer.
这真的很烦人。基本上,在机器。对于正在开发的框架的版本,有一个LocalSqlServer条目。
On my machine, for version 4:
在我的机器上,版本4:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\Machine.config
C:\Windows\ Microsoft.NET \ Framework \ v4.0.30319 \ Config \ Machine.config
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
I found that if I changed the data source part of the connection string to point at my Sql 2005 full server instance, then the error you mentioned went away.
我发现,如果我将连接字符串的数据源部分更改为指向Sql 2005的完整服务器实例,那么您提到的错误就会消失。
(Similar for other versions of the framework, which I also changed)
(类似于框架的其他版本,我也对其进行了修改)
I can't remember if I needed to restart just visual studio or the whole machine before I saw the changes work.
我不记得在看到更改之前是否需要重新启动visual studio或整个机器。
Remember to back up your machine.config files before editing them!
记得备份机器。配置文件之前,编辑他们!
With that being said, there's also no reason why you can't add the database into Sql Server itself (if you have the mdf) then connect to it from Visual Studio via the View -> Server Explorer -> Data Connections (Right Click -> Add Connection) - have you tried that?
既然如此,也没有理由不能将数据库添加到Sql Server本身(如果您有mdf),然后通过视图->服务器资源管理器->数据连接(右键单击->添加连接)从Visual Studio中连接到它,您试过了吗?
#2
1
I know this post is a bit old but i encountered the same problem and i actually found a solution, so i would like to share it.
我知道这篇文章有点过时,但我遇到了同样的问题,我找到了一个解决方案,所以我想和大家分享一下。
- Install sql express 2008 r2
- 安装sql express 2008 r2
- In visual studio 2010 go to
Tools -> Options
- 在visual studio 2010中使用工具->选项。
- Select
Database Tools -> Data Connections
and update theSql Server Instance Name (blank for default)
with the instance name of your database. - 选择Database Tools ->数据连接并使用数据库的实例名更新Sql Server实例名(默认为空白)。
- Then go to services by pressing ⊞Win + R and
services.msc
- 然后去服务按⊞+ R和services.msc获胜
- Select the
SQL Server (<instance name of express edition>)
, right click and selectProperties
- 选择SQL Server (< express edition>的实例名),右键单击并选择Properties
- Then in the properties window of the service go to
Log On
tab and selectLocal System account
- 然后,在服务的属性窗口中,转到Log On选项卡并选择Local System account
After these steps i was able to add a .mdf
file into visual studio 2010.
在这些步骤之后,我能够将.mdf文件添加到visual studio 2010中。
Also maybe is possible to be able to do it without installing Sql server express, just starting from the second step, but i did not try it.
也许还可以在不安装Sql server express的情况下做到这一点,只是从第二步开始,但我没有尝试。
#3
0
you can use code to add that if not exist
如果不存在,可以使用代码添加
string curFile = @"C:\Dev\Test_data.mdf";
if (!File.Exists(curFile))
{
SqlConnection connection = new SqlConnection(@"server=(localdb)\v11.0");
using (connection)
{
connection.Open();
string sql = string.Format(@"
CREATE DATABASE
[Test]
ON PRIMARY (
NAME=Test_data,
FILENAME = '{0}\Test_data.mdf'
)
LOG ON (
NAME=Test_log,
FILENAME = '{0}\Test_log.ldf'
)",
@"C:\Dev"
);
SqlCommand command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
}
}