如何在不安装SQL Server Express Edition的情况下在Visual Studio中添加SQL Ser
本文关键字:SQL Studio Visual 情况下 Ser 添加 Express 不安 安装 Server Edition | 更新日期: 2023-09-27 18:22:36
在Visual Studio 2010项目中添加.mdf
文件(SQL Server数据库)时出现以下错误
连接到SQL Server数据库文件(.mdf)需要SQL Server要安装并运行的2005 Express或SQL Server 2008 Express本地计算机
我不想安装SQL Server Express(2005/2008),因为我已经安装了SQL Server 2005 Enterprise Edition
我正在使用Visual Studio 2010 Ultimate
这真的很烦人。基本上,在针对您开发的框架版本的Machine.config中,有一个LocalSqlServer条目。
在我的机器上,对于版本4:
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" />
我发现,如果我将连接字符串的数据源部分更改为指向我的Sql 2005完整服务器实例,那么您提到的错误就会消失。
(类似于其他版本的框架,我也更改了它)
我不记得在看到更改生效之前,我是需要重新启动visual studio还是整个机器。
编辑machine.config文件之前,请记住备份这些文件!
话虽如此,您也没有理由不能将数据库添加到Sql Server本身(如果您有mdf),然后通过视图->服务器资源管理器->数据连接(右键单击->添加连接)从Visual Studio连接到它-您尝试过吗?
我知道这篇文章有点旧,但我遇到了同样的问题,我实际上找到了解决方案,所以我想分享它。
- 安装sql express 2008 r2
- 在visual studio 2010中转到
Tools -> Options
- 选择
Database Tools -> Data Connections
并使用数据库的实例名更新Sql Server Instance Name (blank for default)
- 然后按⊞Win+R和
services.msc
进入服务 - 选择
SQL Server (<instance name of express edition>)
,右键单击并选择Properties
- 然后在服务的属性窗口中转到
Log On
选项卡并选择Local System account
在这些步骤之后,我能够将.mdf
文件添加到visualstudio2010中。
也可能在不安装Sql-server-express的情况下完成,只是从第二步开始,但我没有尝试。
如果不存在,可以使用代码添加
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();
}
}