Cannot connect to shared instance of LocalDB 2017/2019

atiyar 11 Reputation points
2020-11-29T03:25:54.347+00:00

As a part of my VS2019 installation, I had LocalDB v13 (SQL Server 2016) on my machine. I could share a named instance and I could -

  1. login to the shared instance (in SSMS) with Server Name - (LocalDB)\.\SharedName
  2. access a database from my .NET Core app with connection string -
    "Server=(LocalDB)\.\SharedName;Database=MyDB;Trusted_Connection=True;"
  3. host the app in a Windows Service running under a different user account and access a database with above connection string

Recently, I uninstalled the v13 and installed v15 (SQL Server 2019). Created new instance and shared it as before, but I cannot do anything listed above.

When I try to login through SSMS, I meet with the following message -

43268-ldbshared-failed.png

When I try to access the database from my .NET Core app, an exception says -

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

and similar message in Event Viewer with Window Service hosting.

I tried v14 (SQL Server 2017) too. Same result as v15.

Is there something new with LocalDB v14 and v15 when sharing an instance which I might be missing? I checked the documentation(Connect to a shared instance of LocalDB) but couldn't find anything helpful.

How do I solve this issue? Thanks.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,653 questions
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. AmeliaGu-MSFT 13,961 Reputation points Microsoft Vendor
    2020-12-02T09:54:50.597+00:00

    Hi @atiyar ,
    Sorry for my mistakes.
    After doing some tests, starting from SQL Server 2012 to 2016 SP2, we can use “(LocalDB).\sharedinstancename” to connect it.
    44393-1.jpg
    But since SQL Server 2017 and higher, there is a known issue that we cannot connect to localdb shared instance name.
    44412-2.jpg

    Please refer to this feedback.
    I’m afraid we need to use SQL server 2016 LocalDB shared instance temporarily.
    Best Regards,
    Amelia

    2 people found this answer helpful.

  2. Gustav 712 Reputation points MVP
    2021-04-04T16:24:52.537+00:00

    Oh my ... spent a day struggling with LocalDb 2019, only finally to bump on this frustrating answer.

    Couldn't this important info be raised to the Doc pages of LocalDb?

    1 person found this answer helpful.
    0 comments No comments

  3. AmeliaGu-MSFT 13,961 Reputation points Microsoft Vendor
    2020-11-30T03:20:44.277+00:00

    Hi @atiyar ,

    access a database from my .NET Core app with connection string -"Server=(LocalDB)\.\SharedName;Database=MyDB;Trusted_Connection=True;"

    The server name should be “(LocalDB.\SharedInstancename)”. And Please make sure the shared Instance name is correct.

    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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).

    Please make sure the LocalDB is running.
    You can use SQLLocalDB info <instancename> in cmd to view the state of LocalDB.
    If the LocalDB is stopped, you can use SqlLocalDB start <instancename> in cmd to start the specified instance.
    In addition, SQL Server express LocalDB doesn't accept remote connections, and it supports only local. You cannot connect to SQL Server LocalDB from remote computer.

    If you have any other question, please feel free to let me know.

    Best Regards,
    Amelia


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    What can I do if my transaction log is full?--- Hot issues November
    How to convert Profiler trace into a SQL Server table -- Hot issues November


  4. Johan Greefkes 1 Reputation point
    2022-08-17T14:44:20.347+00:00

    This may be a temporary workaround, but is good enough for my testing:

    1. As the user that owns the LocalDB database, run the command SqlLocalDb info <instancename>
    2. from the output, copy the Instance pipe name. This will look something like this: np:\\.\pipe\LOCALDB#8DF00B42\tsql\query
    3. Use this instance pipe name as the SQL instance name for the other users on the same computer.

    Note that the instance name can change after a reboot. With this as the instance name, I'm able to connect using PowerShell and SQL studio.

    0 comments No comments