March 2008
Intermediate to advanced
982 pages
22h 43m
English
You want to connect to a SQL Server using its IP address instead of its server name.
Use the Network Address and Network Library attributes of the connection string.
The solution 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 SqlConnection object.
The C# code in Program.cs in the project ConnectIPAddressSqlServer is shown in Example 1-6.
Example 1-6. File: Program.cs for ConnectIPAddressSqlServer solution
using System;
using System.Data.SqlClient;
namespace ConnectIPAddressSqlServer
{
class Program
{
static void Main(string[] args)
{
string connectString =
"Network Library=dbmssocn;Network Address=127.0.0.1;" +
"Integrated security=SSPI;Initial Catalog=AdventureWorks";
using (SqlConnection connection = new SqlConnection(connectString))
{
connection.Open( );
// Return some information about the server.
Console.WriteLine(
"ConnectionState = {0}\nDataSource = {1}\nServerVersion = {2}",
connection.State, connection.DataSource,
connection.ServerVersion);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey( );
}
}
} The output is shown in Figure 1-5.

Figure 1-5. Output for ConnectIPAddressSqlServer solution
SQL Server network libraries are dynamic-link libraries (DLLs) that perform network operations required ...
Read now
Unlock full access