@@ -418,6 +418,8 @@ internal class DataSource
418418 private const string LocalDbHost_NP = @"np:\\.\pipe\LOCALDB#" ;
419419 private const string NamedPipeInstanceNameHeader = "mssql$" ;
420420 private const string DefaultPipeName = "sql\\ query" ;
421+ private const string InstancePrefix = "MSSQL$" ;
422+ private const string PathSeparator = "\\ " ;
421423
422424 internal enum Protocol { TCP , NP , None , Admin } ;
423425
@@ -683,12 +685,35 @@ private bool InferNamedPipesInformation()
683685 // If we have a datasource beginning with a pipe or we have already determined that the protocol is Named Pipe
684686 if ( _dataSourceAfterTrimmingProtocol . StartsWith ( PipeBeginning , StringComparison . Ordinal ) || _connectionProtocol == Protocol . NP )
685687 {
686- // If the data source is "np:servername"
688+ // If the data source starts with "np:servername"
687689 if ( ! _dataSourceAfterTrimmingProtocol . Contains ( PipeBeginning ) )
688690 {
689- PipeHostName = ServerName = _dataSourceAfterTrimmingProtocol ;
691+ // Assuming that user did not change default NamedPipe name, if the datasource is in the format servername\instance,
692+ // separate servername and instance and prepend instance with MSSQL$ and append default pipe path
693+ // https://learn.microsoft.com/en-us/sql/tools/configuration-manager/named-pipes-properties?view=sql-server-ver16
694+ if ( _dataSourceAfterTrimmingProtocol . Contains ( PathSeparator ) && _connectionProtocol == Protocol . NP )
695+ {
696+ string [ ] tokensByBackSlash = _dataSourceAfterTrimmingProtocol . Split ( BackSlashCharacter ) ;
697+ if ( tokensByBackSlash . Length == 2 )
698+ {
699+ // NamedPipeClientStream object will create the network path using PipeHostName and PipeName
700+ // and can be seen in its _normalizedPipePath variable in the format \\servername\pipe\MSSQL$<instancename>\sql\query
701+ PipeHostName = ServerName = tokensByBackSlash [ 0 ] ;
702+ PipeName = $ "{ InstancePrefix } { tokensByBackSlash [ 1 ] } { PathSeparator } { DefaultPipeName } ";
703+ }
704+ else
705+ {
706+ ReportSNIError ( SNIProviders . NP_PROV ) ;
707+ return false ;
708+ }
709+ }
710+ else
711+ {
712+ PipeHostName = ServerName = _dataSourceAfterTrimmingProtocol ;
713+ PipeName = SNINpHandle . DefaultPipePath ;
714+ }
715+
690716 InferLocalServerName ( ) ;
691- PipeName = SNINpHandle . DefaultPipePath ;
692717 return true ;
693718 }
694719
0 commit comments