After reviewing a Gert Drappers’s session from TechEd 2004 in Amsterdam, I started to make some tests:Given the method Connect:Private Sub Connect(ByVal server As String)
Dim s As String = “Server=” + server + “;Database=Northwind;Trusted_Connection=SSPI;Application Name=” + server + “;Pooling=false;”
Dim con As New SqlConnection(s)
Try

con.Open()
Console.WriteLine(“ok ; ” + server)
con.Close()
Catch ex As Exception
Console.WriteLine(“Error; ” + server + vbTab + ex.Message)
End Try
End Sub
And the following calls:Connect(“lpc:localhost”)
Connect(“localhost”)
Connect(“np:localhost”)
Connect(“localhost”)
Connect(“tcp:localhost”)
Connect(“localhost”)

I’ve disabled Connection Pooling in order that SQL Server Profiler catchs all the connection requests;

And the results that Profiler has given me are:

Audit Login — network protocol: TCP/IP lpc:localhost 53

Audit Login — network protocol: TCP/IP localhost 53
Audit Login — network protocol: Named Pipes np:localhost 53
Audit Login — network protocol: Named Pipes localhost 53
Audit Login — network protocol: TCP/IP tcp:localhost 53
Audit Login — network protocol: TCP/IP localhost 53

Where the columns are EventClass, TextData, Application Name and SPID.

I get those conclusions:

Each time you connect with servername localhost, regardless the connection protocol you specify, SQL Server with connect with the preview protocol used for the servername localhost; if it haven’t been specified any protocol (in the Windows Registry there is no information about the last connection protocol used) it will connect with TCP/IP.

FYI; I’ve used Regmon utility from sysinternals in order to see the access made to Windows Registry.

http://www.sysinternals.com/ntw2k/source/regmon.shtml

The Registry entry that stores the last protocol used is:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftMSSQLServerClientSuperSocketNetLibLastConnect

And my recomendation is:
Use as servername in connections to the local machine: (local), . (dot) or the servername.

Set in your connection string the protocolo to use; it’s very simple; prefix to the servername the connection protocolo to use plus : like this:
np for Named Pipes,
tcp for TCP/IP and
lpc for Shared Memory.

Notes:
this behavior it does not reproduce in .NET Framework 2.0 🙂
if the servername is (local) or . (dot) and you don’t specify the connection protocol, it works as spected; it will connect with Shared Memory as connection protocol.

Tell me what you think about this …

Eladio Rincón