C# 如何在不安装 SQL Server Express Edition 的情况下在 Visual Studio 中添加 SQL Server 数据库文件 (.mdf)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9127359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add SQL Server database file (.mdf) in Visual Studio without installing SQL Server Express Edition?
提问by Ali
I have an error below adding an .mdffile (SQL Server Database) in a Visual Studio 2010 project
.mdf在 Visual Studio 2010 项目中添加文件(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 企业版
I am using Visual Studio 2010 Ultimate
我正在使用 Visual Studio 2010 Ultimate
采纳答案by dash
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\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!
请记住在编辑之前备份您的 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)然后通过视图从 Visual Studio 连接到它 -> 服务器资源管理器 -> 数据连接(右键单击 - > 添加连接) - 你试过吗?
回答by CodeArtist
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
- In visual studio 2010 go to
Tools -> Options - Select
Database Tools -> Data Connectionsand update theSql Server Instance Name (blank for default)with the instance name of your database. - Then go to services by pressing ?Win+ Rand
services.msc - Select the
SQL Server (<instance name of express edition>), right click and selectProperties - Then in the properties window of the service go to
Log Ontab and selectLocal System account
- 安装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
After these steps i was able to add a .mdffile 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 的情况下做到这一点,只是从第二步开始,但我没有尝试。
回答by David Fawzy
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();
}
}

