简体   繁体   English

如何在Visual Studio中添加SQL Server数据库文件(.mdf)而不安装SQL Server Express Edition?

[英]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. 我知道这篇文章有点老但我遇到了同样的问题,我实际上找到了解决方案,所以我想分享它。

  1. Install sql express 2008 r2 安装sql express 2008 r2
  2. In visual studio 2010 go to Tools -> Options 在visual studio 2010中,转到Tools -> Options
  3. Select 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)
  4. Then go to services by pressing ⊞Win + R and services.msc 然后按⊞Win + Rservices.msc 进入 services.msc
  5. Select the SQL Server (<instance name of express edition>) , right click and select Properties 选择SQL Server (<instance name of express edition>) ,右键单击并选择“ Properties
  6. Then in the properties window of the service go to 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.

相关问题 如何在没有Management Studio的情况下将数据库mdf文件附加到SQL Server Express? - How to attach database mdf file to SQL Server Express without Management Studio? 连接到sql server数据库mdf文件而不在客户机上安装sql server? - Connecting to sql server database mdf file without installing sql server on client machine? 将Visual Studio设置为创建SQL Server 2005支持的mdf文件 - Set Visual Studio to create mdf file that SQL Server 2005 supports 如何使2个应用程序使用SQL Server Express访问相同的mdf文件 - How to get 2 applications accessing same mdf file with SQL Server Express 如何以编程方式备份​​SQL Server 2014 Express Localdb(.mdf)文件 - How to backup a SQL Server 2014 Express Localdb (.mdf) file programmatically 是否可以在没有SQL Server的情况下访问.mdf数据库? - Is it possible to access a .mdf database without SQL Server? 如何查询我在Visual Studio的SQL Server中附加的mdf文件? - How to query against a mdf file I attached in SQL Server in Visual Studio? 将数据库(.mdf文件)附加到SQL Server时出错 - Error attaching a database (.mdf file) to SQL Server 如何检测SQ​​L Server版本是否为Express? - How to detect whether the SQL Server edition is Express? SQL Server Express版本问题 - SQL Server Express edition issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM