Delen via


Creating a Valid Connection String Using TCP/IP

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 Server 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 and SQL Server 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.

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

Examples

Connecting by server name:

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

Connecting by server name to a named instance:

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

Connecting by server name to a specified port:

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

Connecting by IP address:

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

Connecting by IP address to a named instance:

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

Connecting by IP address to a specified port:

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

Connecting to the local computer using (local):

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

Connecting to the local computer using localhost:

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

Connecting to a named instance on the local computer localhost:

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

Connecting to the local computer using a period:

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

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

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.