[tip] localhost vs. (local) in SQL Server connection strings

Sample code with SQL Server connection strings often use localhost and (local) interchangeably. They're different.

Server=(local);Database=DotNetNuke;Trusted_Connection=True
Uses named pipes

Server=localhost;Database=DotNetNuke;Trusted_Connection=True
Uses a TCP port negotiated on port 1434 udp, which defaults to 1433

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.  

As mentioned in a comment on Peter Van Ooijen's blog a while ago, the easiest setup is to avoid either local or localhost and just use the machine name (e.g.  Server=COMPY386;Database=DotNetNuke;Trusted_Connection=True). Note that this will use TCP/IP rather than named pipes. This isn't always practical in a group development situation where each developer is running a SQL Server instance since each machine will have a unique name.

Info on troubleshooting SQL Server 2000 connectivity here.
Also check out this great info on ASPNET connectivity to SQL Server by RupW in the GDN message boards.

Published Friday, December 02, 2005 4:56 AM by Jon Galloway
Filed under: ,

Comments

# re: [tip] localhost vs. (local) in SQL Server connection strings

Also remember that SQL2005 now turns a lot of things off by default. For example Developer Edition install closes TCP connections until you open them with the surface area configuration tool.

Friday, December 02, 2005 2:48 AM by Barry Dorrans

# re: [tip] localhost vs. (local) in SQL Server connection strings

> This isn't always practical in a group development situation where each developer is running a SQL Server instance since each machine will have a unique name

Jon -

Good post...

I usually end up with something along these lines but it leaves me feeling uncomfortable in the event that I need to move the application to a new machine. What do you do?

if (Server.Environment.MachineName == "COMPY386")
{
//use this --> System.Configuration.ConfigurationSettings.AppSettings["PRODUCTION_RDBMS"];
}
else
{
// use this --> System.Configuration.ConfigurationSettings.AppSettings["DEV_RDBMS"];
}

Friday, December 02, 2005 9:15 AM by Vic Berggren

# re: [tip] localhost vs. (local) in SQL Server connection strings

Good post! Would there be much of a performance difference between using each option?

Friday, December 02, 2005 10:15 AM by Joe

# re: [tip] localhost vs. (local) in SQL Server connection strings

Also, keep in mind, there is a performance hit for using named pipes. Named pipes are backed by disk, and too much named pipes usage may cause disk i/o to spike. Combine this with the fact that database servers by nature are disk i/o intensive, it may be better to offload the communication activity to network i/o through the use of TCP.

This was the advice of the SQL Server support team, after identifying perf issues on one of our SQL Servers.

Friday, December 02, 2005 11:43 AM by Jerry Dennany

# re: [tip] localhost vs. (local) in SQL Server connection strings

I've always wondered this but was too lazy to ask.

Friday, December 02, 2005 12:58 PM by Haacked

# re: [tip] localhost vs. (local) in SQL Server connection strings

I always thought (local) uses "shared memory" not named pipes. I always turn of named pipes + tcp for my development hosts, and everything works fine.

I also think it is a myth, that named pipes caus disk I/O as said in a comment. Just because something uses a file-like interface does not necessarily mean it accesses the disk - only scsi/ide and other DISK drivers access the disk.

Friday, December 02, 2005 2:08 PM by Harald Ums

# re: [tip] localhost vs. (local) in SQL Server connection strings

Just a comment to Vic:

One nice way around this is to use the <appSettings file=""> functionality in app.config files. This way you can have a config file for each dev and production machine and not actually have to change your config file itself, or the code using it.

Also, if file="" is used, the app settings in the included file override the same named settings in the app.config file.

Very useful!

Wednesday, December 14, 2005 7:00 AM by Mladen Mihajlovic

# re: [tip] localhost vs. (local) in SQL Server connection strings

Have a look at the following blog entry:
http://blogs.msdn.com/sql_protocols/archive/2005/10/29/486861.aspx

It says that when using MDAC and OLEDB, the "(local)" and "localhost" use TCP connection, but <machinename> use a shared memory connection.

Thursday, December 15, 2005 4:07 PM by Scott

# re: [tip] localhost vs. (local) in SQL Server connection strings

for all connection string you have found here on this site

http://www.connstr.com

Tuesday, June 20, 2006 11:05 AM by information officer

# Link-O-Rama (December 2005)

Another month. Another collection of links... Sports/Packers/Racing Turiaf will play for Yakama Sun Kings

Thursday, September 14, 2006 2:44 AM by Rick.Stavanja.com

# re: [tip] localhost vs. (local) in SQL Server connection strings

You are a life saver.. computer nave over local or localhost is exactly what I needed to know.

Saturday, November 18, 2006 10:53 PM by Josh

# re: [tip] localhost vs. (local) in SQL Server connection strings

is there a difference in using server=local and server=(local) ?

Tuesday, April 17, 2007 9:10 PM by Past

# Tip: Put Connection Strings in Their Own Configuration File

Tip: Put Connection Strings in Their Own Configuration File

Thursday, June 07, 2007 6:46 PM by StevenHarman.net

# Installed SQL 2005

Critical Error: SiteUrls.Config The file containing the SiteUrl Data could not be loaded. Please contact

Saturday, June 16, 2007 7:32 PM by Yoda's Blog

# re: [tip] localhost vs. (local) in SQL Server connection strings

Where does using "." fit into the equation? Is it the same as local or localhost?

Tuesday, August 28, 2007 10:03 AM by Carla

# re: [tip] localhost vs. (local) in SQL Server connection strings

All I know is that localhost uses TCPIP while (local) and . use a protocol called shared memory and does not use any network protocols.  Am I right on this?

Thursday, November 29, 2007 11:09 PM by oil painting portraits

# re: [tip] localhost vs. (local) in SQL Server connection strings

Hi,

sqlDataAdapter wizzrd only connecting one database from sql server 2005 in my pc. Why not other databeses. I have no password or user id.

Is there any default setting that I have to change?

I will appreciate if I have info.

thanks,

Gita

Thursday, February 07, 2008 12:09 PM by GITA

# re: [tip] localhost vs. (local) in SQL Server connection strings

want to know, how to connect to a sql server from serversystem to anther system which is in lan with server but has no sqlserver installed before.

plz send me the solution in detail

Monday, June 09, 2008 12:44 AM by nani

# re: [tip] localhost vs. (local) in SQL Server connection strings

Quote:"All I know is that localhost uses TCPIP while (local) and . use a protocol called shared memory and does not use any network protocols. "

We use the "." connection to connect to local SQL, but if the customer connects the computer to a Network and then physically disconnects from the network our program encounters SQL Network errors. We cannot hard code the computer name and really just want to use Named Pipes or Shared Memory. Is there way to specify Named Pipes?

Tuesday, July 01, 2008 4:06 PM by popTart992000

# re: [tip] localhost vs. (local) in SQL Server connection strings

I found this on the MSDN site:

"By default, clients have TCP and Named Pipes as available protocols. You can manipulate the protocol ordering by using the SQL Server Client utility. The client application uses the protocols in the order specified on the client computer. The protocol order is stored at the following registry key location under the value ProtocolOrder: HKLM\Software\Microsoft\MSSQLServer\Client\SuperSocketNetLib

For example, if a client computer has both TCP and Named Pipes available, and the order is:

• TCP

• Named Pipes "

The reg key for SQL Express 2005 is a little different but I found 'SuperSocketNetLib' key and changed the order to NP, TCP. But our program still encounters a network error accessing the local SQL when the network cable is unplugged.

Any suggestions?

Tuesday, July 01, 2008 4:25 PM by popTart992000

# Alkampfer&#8217;s Place &raquo; Blog Archives &raquo; Really strange error in production server, a tale of named pipes

Pingback from  Alkampfer&#8217;s Place  &raquo; Blog Archives   &raquo; Really strange error in production server, a tale of named pipes

Leave a Comment

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