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.
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 SQL Server using its IP address. Information about the SQL Server is displayed from the properties of the
SqlConnectionobject.
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 SQL Server computers to communicate. A server can monitor multiple libraries simultaneously; the only requirement is that each network ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access