[SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string

Barry Dorrans recently mentioned that you can force the database connection protocol by specifying np: or tcp: before the server name in your connection string. I've jumped through some hoops before using localhost to target tcp and (local) to target named pipes, but it looks like there's a much better way to do this (since MDAC 2.6).

There's more info in MS KB Article 313295:

TCP/IP:

server=tcp:hostname
You can optionally specify a specific port number. By default, the port is 1433.
server=tcp:hostname, portNumber

Named Pipes:

server=np:hostname
You can optionally specify a specific named pipe.
server=np:\\hostname\pipe\pipeName

They recommend that you always use (local) to specify the local machine, and then specify a protocol. Sounds like the right way to connect to the local machine over TCP is to specify server=tcp:(local), and to use named pipes on the local machine you'd use np:(local). To specify a protocol when pointing to a server, you'd use server=tcp:DBSERVERNAME.

To quote my previous post:

There are many differences between TCP and Named Pipe connection, but if you're on localhost you're mostly concerned with simple access.

  • The default ASPNET account generally has an easier time with TCP, since the  ASPNET user doesn't have access to named pipes by default (http://support.microsoft.com/Default.aspx?id=315159).
  • If you're using impersonation, Named Pipes is usually simpler. If you're using impersonation with no username specified, you're running under the IIS Authenticating user. This defaults to IUSR_MACHINENAME if you're allowing annonymous access, which generally has access to the IPC$ share required for named pipe communications.  

You can target other supported protocols, too:

Multiprotocol = rpc
Shared Memory = lpc
NWlink  IPX / SPX = spx
Banyan VINES = vines
Apple Talk = adsp

Published Saturday, February 24, 2007 7:09 PM by Jon Galloway

Comments

# re: [SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string

Don't forget that SQL2005 has TCP/IP (and named pipes) disabled by default. So if you force a protocol you do need to enable it, even when attaching to localhost

Sunday, February 25, 2007 2:49 AM by barryd

# re: [SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string

You can also use the "Network Library=" option in the connection string. From http://support.microsoft.com/kb/238949

dbnmpntw - Win32 Named Pipes

dbmssocn - Win32 Winsock TCP/IP

dbmsspxn - Win32 SPX/IPX

dbmsvinn - Win32 Banyan Vines

dbmsrpcn - Win32 Multi-Protocol (Windows RPC)

This is supported all the way back to MDAC 1.5!

Tuesday, February 27, 2007 12:54 PM by Mark

# re: [SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string

Jon, you are the man! As important a thing as Connection Strings are I always forget what syntax to use when... and I often refer to http://connectionstrings.com/ to get the skinny.

But I've never seen (or at least never noticed) the syntax for forcing a particular protocol, and it came in really useful today for a project I was working on.

Thursday, March 08, 2007 3:25 PM by Steve Harman

# re: [SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string

This blog post is still helping developers more than a year later. ;-)

Friday, March 21, 2008 4:46 PM by Mark Wisecarver

# re: [SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string

My question is in a production server environment, is it preferable to force tcp/ip or let the sqlclient decide?

Thursday, July 24, 2008 7:08 PM by Jason Sirota

# re: [SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string

I found weblogs.asp.net very informative. The article is professionally written and I feel like the author knows the subject very well. weblogs.asp.net keep it that way.

Tuesday, June 30, 2009 5:36 PM by bad credit cash advance

# Sql Server Bits and Bats

Pingback from  Sql Server Bits and Bats

Tuesday, September 01, 2009 2:35 PM by Sql Server Bits and Bats

# re: [SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string

Keep in mind that the only method to secure yourself from irritative phone calls and spy devices is to use <a href="www.jammer-store.com/">Cell blocker</a>. Block cell phones around you.

Monday, October 26, 2009 9:58 AM by Mobile blocker

Leave a Comment

(required) 
(required) 
(optional)
(required)