27

I have a C# console application written using Visual Studio 2012. In the application I am using a Sql Server localdb connection to a database to store information. This is working fine on several computers, all of which have Visual Studio installed.

I would like to deploy a program that only has to install the Sql Server Express LocalDB, and not the larger Sql Server Express. However, my application is not running on the target computers. I have installed Sql Server Express LocalDB 2014 on a target computer. I can, using a command line, run commands using sqllocaldb to verify that it is installed and running.

C:\Users\someuser\Desktop\Debug>sqllocaldb v
Microsoft SQL Server 2014 (12.0.2000.8)`

When I run my application on that same target computer, however, I get the following error.

C:\Users\someuser\Desktop\Debug>Testing_Console
11:21:07,912 [1] INFO TestingConsole.Program - Current Directory is C:\Users\someuser\Desktop\Debug
Extra Info: (null)


Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.

The following is the beginning of my app.config file, where I am defining the connection string. I have tried putting in the direct file path to the LM file, but that didn't fix the issue. That was to be expected, however, as the program works from any directory on the computers with Visual Studio installed.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <connectionStrings>
    <add name="KomoLM_Console.Properties.Settings.LMConnectionString"
      providerName="System.Data.SqlClient"
      connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LM.mdf;Integrated Security=True;MultipleActiveResultSets=True"
 />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

I don't know if the issue is related to only have SQL Server Express LocalDB 2014 installed. Can anyone tell me what my problem might be?

Stas Ivanov
  • 1,173
  • 1
  • 14
  • 24
MDig
  • 649
  • 1
  • 6
  • 10
  • possible duplicate of [SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance](http://stackoverflow.com/questions/26248293/sql-network-interfaces-error-50-local-database-runtime-error-occurred-canno) – emerson.marini Jan 07 '15 at 18:38
  • 1
    The problem was related to have Sql Server Express LocalDB 2014 installed. With that version MS has changed the connection string requirements. Instead of "Data Source=(LocalDB)\V11.0", the connection string is "Data Source=(LocalDB)\MSSQLLocalDB". After changing my connection string the program is running correctly on a computer that only has the LocalDB 2014 installed. Here is a link to an article about it: [link](https://connect.microsoft.com/SQLServer/feedback/details/845278/sql-server-2014-express-localdb-does-not-create-automatic-instance-v12-0) – MDig Jan 07 '15 at 18:38
  • **For Code Migrations** (`update-database` et al) [see this answer](https://stackoverflow.com/a/31266905/3258851). – Marc.2377 Jul 31 '18 at 21:16

7 Answers7

35

The problem was related to having Sql Server Express LocalDB 2014 installed instead of 2012. With that version MS has changed the connection string requirements. Instead of Data Source=(LocalDB)\V11.0, the connection string is Data Source=(LocalDB)\MSSQLLocalDB. After changing my connection string the program is running correctly on a computer that only has the LocalDB 2014 installed. Here is a link to an article about it: https://connect.microsoft.com/SQLServer/feedback/details/845278/sql-server-2014-express-localdb-does-not-create-automatic-instance-v12-0

also

http://msdn.microsoft.com/en-us/library/hh510202(v=sql.120).aspx

MDig
  • 649
  • 1
  • 6
  • 10
  • For me the instance name (LocalDB)\MSSQLLocalDB didn't work. Anyway, thanks for the attempt :D – andrea.rinaldi Sep 12 '15 at 12:50
  • I have Microsoft SQL Server 2014 installed and the "(LocalDB)\MSSQLLocalDB" path solved the problem. – user2134488 Oct 03 '16 at 17:46
  • 2
    `MSSQLLocalDB` is simply the name of an instance. You can create as many instances as you need named as you wish. Open `cmd` and enter `sqllocaldb i`. This lists the availble instances. Create a new one using: `sqllocaldb c YouNewInstance`. In an earlier version LocalDB automatically created an instance named `v11.0` in current version this default instance is named `MSSQLLocalDB` – Bernhard Döbler Apr 14 '17 at 20:08
8

Using "Data Source=(LocalDB)\MSSQLLocalDB" also not worked form me. I had to access databasseusing "Data Source=(LocalDB)\V12.0" and for working that access to work I needed to run this command first "sqllocaldb create "v12.0". More details on this link https://dyball.wordpress.com/2014/04/28/sql-2014-localdb-error-cannot-connect-to-locaidbv12-o/

Manoj Attal
  • 2,806
  • 3
  • 28
  • 31
  • 1
    Thanks! The same worked for me but I created the instance named MSSQLLocalDB running "sqllocaldb create MSSQLLocalDB". I suppose it doesn't create it by default if one doesn't have VS installed on the same machine with SQL server. In my case this was a full SQL server installation (not express) – axk Jul 20 '16 at 17:28
4

You'll want to make sure that you've installed .NET Framework 4.0 and, equally as importantly, the .NET Framework 4.0.2 update (KB #2544514).

Once your system is up to date, you can download the SqlLocalDb installer from:

http://www.microsoft.com/en-us/download/details.aspx?id=29062

2

The build having connection string of (LocalDB)\v11.0 will work with the localDB ENU\x64\SqlLocalDB.MSI given on this link Download SqlLocalDB

I tried this on target system where no Visual Studio is installed. This build will connect with the database with only SqlLocalDB.msi installed. There is no need to install SqlExpress on target system.

2

The problem is when you have both Visual Studio 2013 and Visual Studio 2017 there are two versions of loacal database installed.

Visual Studio 2013 - (localdb)\v11.0 Microsoft SQL Server 2012 (SP1) - 11.0.3000.0 (X64) Oct 19 2012 13:38:57 Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows NT 6.2 (Build 9200: )

Visual Studio 2017 - (localdb)\MSSQLLOCALDB Microsoft SQL Server 2016 (SP1) (KB3182545) - 13.0.4001.0 (X64) Oct 28 2016 18:17:30 Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows 10 Pro 6.3 (Build 17134: )

Before installing Visual Studio 2017 I was able to connect to (localdb)\v11.0, but after installing Visual Studio 2017 I am not able to connect to previous version of SQL Express (localdb)\v11.0, but I am able to connect to (localdb)\MSSQLLOCALDB using C#. I am able to connect to both of them from SQL Server Management Studio without any issues.

Bharath
  • 115
  • 1
  • 8
1

For me deleting and re-creating the MSSQLLocalDB solved the issue:

  1. Locate the most recent SqlLocalDB version:

    DIR "C:\Program Files\Microsoft SQL Server\sqllocaldb.exe" /S /B

  2. Move into the directory with the highest version number, e.g.

    CD "C:\Program Files\Microsoft SQL Server\150\Tools\Binn\"

  3. Delete default instance of LocalDB:

    SqlLocalDB.exe delete MSSQLLocalDB

  4. Re-create default instance of LocalDB:

    SqlLocalDB.exe create MSSQLLocalDB

Christoph
  • 3,322
  • 2
  • 19
  • 28
0

I took the following steps to connect to the SQL Local DB and it perfectly works for me:

  1. Download and install Download Microsoft SQL Server Management Studio (SSMS) 19: from the Microsoft official Site: SSMS

  2. Download and install SQL Server 2022 from Microsoft Site: SQL Server

  3. After SQL Server Installation you will get the server name from the connection string: in my case, it was: " localhost\MSSQLSERVER01 "

1

  1. Open Microsoft SQL Server Management Studio and follow the steps: connect> Database Engine > server name: <server name from step 3> connect.

Hope this will fix the issue.

Dordi
  • 778
  • 1
  • 5
  • 14