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

柔情痞子 提交于 2019-12-18 12:38:36

问题


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"

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.

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?)


回答1:


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.

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. Using the sqllocaldb commandline tool:

SqlLocalDB.exe create "MyNamedInstance" 12.0 -s

The -s parameter starts the instance immediately.

  1. Specifying the named instance in app.config:

For this add to the <configSections> tag:

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



回答2:


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

And then create that instance name on 2104 (version 12.0) using:

sqllocaldb create "mssqllocaldb" 12.0

There is also a nice .NET library available for doing this:

https://github.com/martincostello/sqllocaldb



来源:https://stackoverflow.com/questions/39974109/how-can-i-specify-to-use-sql-server-localdb-2014-rather-than-sql-server-localdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!