Creating a Valid Connection String Using TCP/IP

SQL Server Configuration Manager

To create a valid connection string using TCP/IP, you must:

  • Specify an Alias Name.
  • For the Server, enter either a server name to which you can connect using the PING utility, or an IP address to which you can connect using the PING utility. For a named instance append the instance name.
  • Specify TCP/IP for the Protocol.
  • Optionally, enter a port number for the Port No. The default is 1433, which is the port number of the default instance of the Database Engine on a server. To connect to a named instance or a default instance that is not listening on port 1433, you must provide the port number, or start the SQL Server Browser service. For information on configuring the SQL Server Browser service, see SQL Server Browser Service.

At the time of connection, the SQL Native Client component reads the server, protocol, and port values from the registry for the specified alias name, and creates a connection string in the format tcp:<servername>[\<instancename>],<port> or tcp:<IPAddress>[\<instancename>],<port>.

Note:
Microsoft Windows XP Service Pack 2 enables Windows Firewall, which closes port 1433 by default. Because Microsoft SQL Server communicates over port 1433, you must reopen the port if SQL Server is configured to listen for incoming client connections using TCP/IP. For information on configuring a firewall, see "How to: Configure a Firewall for SQL Server Access" in SQL Server Books Online or review your firewall documentation.

SQL Server 2005 and SQL Native Client fully support both Internet Protocol version 4 (IPv4) and Internet Protocol version 6 (IPv6). SQL Server Configuration Manager accepts both IPv4 and IPv6 formats for IP addresses. For information on IPv6, see "Connecting Using IPv6" in SQL Server Books Online.

Connecting to the Local Server

When connecting to SQL Server running on the same computer as the client, you can use (local) as the server name. This is not encouraged as it leads to ambiguity, however it can be useful when the client is known to be running on the intended computer. For instance, when creating an application for mobile disconnected users, such as a sales force, where SQL Server will run on laptop computers and store project data, a client connecting to (local) would always connect to the SQL Server running on the laptop. The word localhost or a period (.) can be used in place of (local).

Verifying Your Connection Protocol

The following query returns the protocol used for the current connection.

Copy Code
SELECT net_transport 
FROM sys.dm_exec_connections 
WHERE session_id = @@SPID;

Examples

Connecting by server name:

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             <servername>

Connecting by server name to a named instance:

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             <servername>\<instancename>

Connecting by server name to a specified port:

Copy Code
Alias Name         <serveralias>
Port No            <port>
Protocol           TCP/IP
Server             <servername>

Connecting by IP address:

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             <IPAddress>

Connecting by IP address to a named instance:

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             <IPAddress>\<instancename>

Connecting by IP address to a specified port:

Copy Code
Alias Name         <serveralias>
Port No            <port number>
Protocol           TCP/IP
Server             <IPAddress>

Connecting to the local computer using (local):

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             (local)

Connecting to the local computer using localhost:

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             localhost

Connecting to a named instance on the local computer localhost:

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             localhost\<instancename>

Connecting to the local computer using a period:

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             .

Connecting to a named instance on the local computer using a period:

Copy Code
Alias Name         <serveralias>
Port No            <blank>
Protocol           TCP/IP
Server             .\<instancename>
Note:
For information on specifying the network protocol as a sqlcmd parameter, see "How to: Connect to the Database Engine Using sqlcmd.exe" in SQL Server Books Online.

See Also