简体   繁体   English

如何在连接字符串中指定使用 SQL Server LocalDb 2014 而不是 SQL Server LocalDb 2016?

[英]How can I specify to use SQL Server LocalDb 2014 rather than SQL Server LocalDb 2016 in the connection string?

Our application uses SQL Server LocalDb 2014 as the database engine.我们的应用程序使用 SQL Server LocalDb 2014 作为数据库引擎。 The connection string we use is我们使用的连接字符串是

"Data Source=(localdb)\MSSQLLOCALDB;Initial Catalog=OurDatabase;MultipleActiveResultSets=True;Integrated Security=True;AttachDBFilename=|DataDirectory|OurDatabase.mdf"

Now, on just one of our computers, it has VS 2015SP3 and the latest version of the SQL Server objects installed, our application starts using SQL Server LocalDb 2016. This is undesirable as we exchange back-ups of the database files regularly between computers and now the back-ups that are made in the LocalDb 2016 format cannot be read on computers that do not have LocalDb 2016.现在,在我们的其中一台计算机上安装了 VS 2015SP3 和最新版本的 SQL Server 对象,我们的应用程序开始使用 SQL Server LocalDb 2016。这是不可取的,因为我们在计算机和计算机之间定期交换数据库文件的备份现在,在没有 LocalDb 2016 的计算机上无法读取以 LocalDb 2016 格式制作的备份。

The problem is that the connection string does not specify which version of LocalDb should be used.问题是连接字符串没有指定应该使用哪个版本的 LocalDb。 It there a way to force LocalDb 2014 (or 2016, if we decide to upgrade?)有没有办法强制 LocalDb 2014(或 2016,如果我们决定升级?)

Well, seeing that apart from Erik's answer no solutions have been provided, we must assume that indeed you cannot specify which flavour of SQL Server LocalDb you wish to use when using "Data Source=(localdb)\\mssqllocaldb" in a connection string.好吧,看到除了 Erik 的回答之外没有提供任何解决方案,我们必须假设您确实无法指定在连接字符串中使用"Data Source=(localdb)\\mssqllocaldb"时希望使用的 SQL Server LocalDb 的风格。

A drawback of Erik's solution is that it does not play nice with other applications that may use the default instance of LocalDb (MSSQLLocalDb). Erik 解决方案的一个缺点是,它与其他可能使用 LocalDb 的默认实例 (MSSQLLocalDb) 的应用程序不兼容。 I found a different approach in using a so called named instance: an instance of LocalDb private to your application.我发现了一种使用所谓命名实例的不同方法:您的应用程序私有的 LocalDb 实例。 While defining a named instance you can specify the version of LocalDb you want to use: 12.0 for LocaldDb 2014, 13.0 for Localdb 2016.在定义命名实例时,您可以指定要使用的 LocalDb 版本:LocaldDb 2014 为 12.0,Localdb 2016 为 13.0。

There are two ways to create a named instance:有两种方法可以创建命名实例:

  1. Using the sqllocaldb commandline tool:使用sqllocaldb命令行工具:

SqlLocalDB.exe create "MyNamedInstance" 12.0 -s

The -s parameter starts the instance immediately. -s参数立即启动实例。

  1. Specifying the named instance in app.config:在 app.config 中指定命名实例:

For this add to the <configSections> tag:为此添加到<configSections>标签:

<section name="system.data.localdb"
         type="System.Data.LocalDBConfigurationSection,System.Data,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"/>

Then add a new tag:然后添加一个新标签:

<system.data.localdb>
    <localdbinstances>
      <add name="MyNamedInstance" version="12.0" />
    </localdbinstances>
</system.data.localdb>

You can now specify the named instance in the connection string thus:您现在可以在连接字符串中指定命名实例,因此:

"Data Source=(localdb)\mynamedinstance" 

You can use the sqllocaldb command line tool to create and delete instances, so delete the instance on 2016 (version 13.0) like this:可以使用sqllocaldb命令行工具创建和删除实例,所以在2016(13.0版)上删除实例如下:

sqllocaldb delete "mssqllocaldb"

And then create that instance name on 2014 (version 12.0) using:然后使用以下命令在 2014(版本 12.0)上创建该实例名称:

sqllocaldb create "mssqllocaldb" 12.0

There is also a nice .NET library available for doing this:还有一个不错的 .NET 库可用于执行此操作:

https://github.com/martincostello/sqllocaldb https://github.com/martincostello/sqllocaldb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server 2016 LocalDb连接问题 - SQL Server 2016 LocalDb connection issue 连接到SQL Server 2016 LocalDB的默认实例 - Connection to Default Instance of SQL Server 2016 LocalDB 安装SQL Server 2014 Express LocalDB之后,如何解决“ LOCALDB_ERROR_AUTO_INSTANCE_CREATE_FAILED”错误? - How can I resolve the “LOCALDB_ERROR_AUTO_INSTANCE_CREATE_FAILED” error after installing SQL Server 2014 Express LocalDB? 对LocalDB的SQL Server连接字符串进行故障排除 - Troubleshooting SQL Server connection string for LocalDB 创建连接字符串并使用SQL Server LocalDB - Creating a Connection String and Working with SQL Server LocalDB 无法建立到 SQL Server LocalDB 的连接 - Can't establish a connection to SQL Server LocalDB 如何以编程方式备份​​SQL Server 2014 Express Localdb(.mdf)文件 - How to backup a SQL Server 2014 Express Localdb (.mdf) file programmatically 无法连接到SQL Server 2016 LocalDB - Unable to connect to SQL Server 2016 LocalDB 我可以使用SQL Server项目创建/管理LocalDB文件吗? - Can I use a SQL Server project to create/manage a LocalDB file? 如何指定我的应用程序应使用跟踪标志参数启动 SQL Server LocalDb? - How can I specify that my app should start SQL Server LocalDb with a trace flag parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM