[ Team LiB ]
Recipe 1.6 Using anIPAddresstoConnecttoSQLServer
Problem
You want toconnectto a SQLServerusing its IPaddress instead of its server name.
Solution
Use the Network Address and Network Library attributes of the connection string.
The sample code contains a single event handler:
Connect Button.Click
Creates and opens a connection to a SQLServerusing its IP address. Information
about the SQLServer is displayed from the properties of the SqlConnection
object.
The C# code is shown in Example 1-5
.
Example 1-5. File: ConnectSqlServerIpAddressForm.cs
// Namespaces, variables, and constants
using System;
using System.Data.SqlClient;
// . . .
private void connectButton_Click(object sender, System.EventArgs e)
{
String connString =
"Network Library=dbmssocn;Network Address=127.0.0.1
;" +
"Integrated security=SSPI;Initial Catalog=Northwind";
SqlConnection conn = new SqlConnection(connString);
conn.Open( );
// Return some information about the server.
resultTextBox.Text =
"ConnectionState = " + conn.State + Environment.NewLine +
"DataSource = " + conn.DataSource + Environment.NewLine +
"ServerVersion = " + conn.ServerVersion + Environment.NewLine;
conn.Close( );
resultTextBox.Text += "ConnectionState = " + conn.State;
}
Discussion
SQL Server network libraries are dynamic-link libraries (DLLs) that perform network
operations required for client computers and SQLServer computers to communicate. A
server can monitor multiple libraries simultaneously; the only requirement is that each
network library to be monitored is installed and configured.
Available network libraries for SQLServer 2000 include:
AppleTalk ADSP
Allows Apple Macintosh to communicate with SQLServerusing native
AppleTalk protocol.
Banyan VINES
Supports Banyan VINES Sequenced Packet Protocol (SPP) across Banyan VINES
IP network protocol.
Multiprotocol
Automatically chooses the first available network protocol to establish a
connection generally with performance comparable tousing a native network
library. TCP/IP Sockets, NWLink IPX/SPX, and Named Pipes are supported.
N
amed Pipes
Interprocess communication (IPC) mechanism provided by SQLServer for
communication between clients and servers.
N
WLink IPX/SPX
The native protocol of Novell Netware networks.
TCP/IP Sockets
Uses standard Windows sockets to communicate across the TCP/IP protocol.
Clustered installations of SQLServer support only Named Pipes and TCP/IP protocols.
AppleTalk, Banyan Vines, and Multiprotocol protocols are unavailable if named
instances are installed on the server.
For more information about network libraries and configuring network libraries, see
Microsoft SQLServer Books Online.
The use of the SQLServer TCP/IP Sockets improves performance and scalability with
high volumes of data. It avoids some security issues associated with named pipes. As
with any protocol, the client and the server must be configured to use TCP/IP.
To connecttoSQLServerusinganIP address, the TCP/IP network library must be used
to connectto the SQL Server. This is done by specifying the library in the connection
string as either the attribute Net or Network Library with a value of dbmssocn. Specify
the IPaddressusing the Data Source, Server, Address, Addr, or Network Address
parameter. The following connection string demonstrates using an IPaddressto specify
the data source:
N
etwork Library=dbmssocn;Network Address=127.0.0.1;
Integrated security=SSPI;Initial Catalog=Northwind
In the example, the IPaddress is the local machine. This could also be specified as
(local). To specify a SQLServer other than a local instance, specify the IPaddress of the
computer on which SQLServer is installed.
Default instances of SQLServer listen on port 1433. Named instances of SQLServer
dynamically assign a port number when they are first started. The example above does
not specify the port number and therefore uses the default port 1433 of the SQL Server. I
f
the SQLServer is configured to listen on another port, specify the port number following
the IPaddress specified by the Network Address attribute separated by a comma as
shown in the following snippet, which connects to a local SQLServer listening on port
1450:
N
etwork Address=(local),1450
[ Team LiB ]
. ]
Recipe 1.6 Using an IP Address to Connect to SQL Server
Problem
You want to connect to a SQL Server using its IP address instead of its server name As
with any protocol, the client and the server must be configured to use TCP /IP.
To connect to SQL Server using an IP address, the TCP /IP network library