[英]How to add SQL Server database file (.mdf) in Visual Studio without installing SQL Server Express Edition?
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
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.
基本上,在您正在开发的框架版本的Machine.config中,有一个LocalSqlServer条目。
On my machine, for version 4: 在我的机器上,对于版本4:
C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Config\\Machine.config C:\\ WINDOWS \\ Microsoft.NET \\框架\\ 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! 请记住在编辑之前备份machine.config文件!
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),然后通过View - > Server Explorer - > Data Connections(右键单击 - 从Visual Studio连接到它)。 >添加连接) - 你试过吗?
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. 我知道这篇文章有点老但我遇到了同样的问题,我实际上找到了解决方案,所以我想分享它。
Tools -> Options
Tools -> Options
Database Tools -> Data Connections
and update the Sql Server Instance Name (blank for default)
with the instance name of your database. Database Tools -> Data Connections
并使用Database Tools -> Data Connections
Sql Server Instance Name (blank for default)
更新Sql Server Instance Name (blank for default)
。 services.msc
services.msc
进入 services.msc
SQL Server (<instance name of express edition>)
, right click and select Properties
SQL Server (<instance name of express edition>)
,右键单击并选择“ Properties
Log On
tab and select Local 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的情况下完成它,只是从第二步开始,但我没有尝试。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.