如何在连接字符串中指定使用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?

查看:234
本文介绍了如何在连接字符串中指定使用SQL Server LocalDb 2014而不是SQL Server LocalDb 2016?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序使用SQL Server LocalDb 2014作为数据库引擎.我们使用的连接字符串是

Our application uses SQL Server LocalDb 2014 as the database engine. The connection string we use is

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

现在,在我们的其中一台计算机上,它已安装了VS 2015SP3和最新版本的SQL Server对象,我们的应用程序开始使用SQL Server LocalDb2016.这是不受欢迎的,因为我们定期交换数据库文件的备份在计算机之间,现在无法在没有LocalDb 2016的计算机上读取以LocalDb 2016格式进行的备份.

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.

问题在于连接字符串未指定应使用哪个版本的LocalDb.有一种方法可以强制LocalDb 2014(或2016,如果我们决定升级的话?)

The problem is that the connection string does not specify which version of LocalDb should be used. It there a way to force LocalDb 2014 (or 2016, if we decide to upgrade?)

推荐答案

好吧,看到除了Erik的答案之外,没有提供解决方案,我们必须假设您确实无法指定希望在何时使用哪种SQL Server LocalDb风格.在连接字符串中使用"Data Source=(localdb)\mssqllocaldb".

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解决方案的一个缺点是,它不能与可能使用LocalDb的默认实例(MSSQLLocalDb)的其他应用程序配合使用.在使用所谓的命名实例时,我发现了另一种方法:您的应用程序专用的LocalDb实例.定义命名实例时,您可以 指定要使用的LocalDb版本:LocaldDb 2014为12.0,Localdb 2016为13.0.

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). I found a different approach in using a so called named instance: an instance of LocalDb private to your application. 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.

有两种创建命名实例的方法:

There are two ways to create a named instance:

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

SqlLocalDB.exe create "MyNamedInstance" 12.0 -s

-s参数立即启动实例.

  1. 在app.config中指定命名实例:

为此,请添加到<configSections>标签:

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

然后添加一个新标签:

<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" 

这篇关于如何在连接字符串中指定使用SQL Server LocalDb 2014而不是SQL Server LocalDb 2016?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆