SqlClient: LocalDB shared instance not supported

Connection to a shared instance of localdb fails. Shared instances use names in format ‘.\name’. Hence, a connection string contains a datasource like ‘(localdb)\.\name’ with two backslashes.

But here the assumption is made, that splitting the datasource value by backslash yields exactly two tokens.

string[] tokensByBackSlash = workingDataSource.Split(BackSlashCharacter);

error = false;

// All LocalDb endpoints are of the format host\instancename where host is always (LocalDb) (case-insensitive)
if (tokensByBackSlash.Length == 2 && LocalDbHost.Equals(tokensByBackSlash[0].TrimStart()))
{

Exception message

Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started.

The exception message is a bit misleading. The initial error is, that the datasource name is not recognized as a LocalDB instance.

Stack trace

System.Exception: Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started.
       ---> System.Net.Sockets.SocketException (11001): No such host is known.
         at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses)
         at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
         at System.Net.Sockets.UdpClient.GetEndpoint(String hostname, Int32 port)
         at System.Net.Sockets.UdpClient.SendAsync(Byte[] datagram, Int32 bytes, String hostname, Int32 port)
         at Microsoft.Data.SqlClient.SNI.SSRP.SendUDPRequest(String browserHostname, Int32 port, Byte[] requestPacket)
         at Microsoft.Data.SqlClient.SNI.SSRP.GetPortByInstanceName(String browserHostName, String instanceName)

To reproduce

Try to connect to a shared instance. From what I saw, it does not even have to exist to let you reproduce the error. Just use a connection string with the ‘.\name’ syntax:

var _connectionString = "Data Source=(localdb)\\.\\MyLocalDB_Shared;Initial Catalog=MyTable;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;"

var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

Expected behavior

The data source should be recognized as LocalDB instance.

Further technical details

Microsoft.Data.SqlClient version: 3.0.0 .NET target: 5.0 SQL Server version: 15.0.4153 with cumulative update SQLServer2019-KB5004524-x64.exe installed Operating system: Windows 10 Pro, version 2004, build 19041.1165

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (14 by maintainers)

Most upvoted comments

@ErikEJ there are two types of instances (MS doc):

  1. Automatic
  2. Named

When we use (localdb)\. the connection will be established with the automatic (default) LocalDB instance.

@JRahnama

Thank you, that’s it! With this artifact I could successfully connect to shared instance.

one more question are you using managed SNI by any chance?