UseNetwork AddressAndNetwork LibraryThe connection string of the property.
The following solution shows and solves the problem of connecting to the database through an IP address.
1 Using System;
2 Using System. Data. sqlclient;
3
4 Namespace Connectipaddresssqlserver
5 {
6 Class Program
7 {
8 Static Void Main ( String [] ARGs)
9 {
10 String Connectstring =
11 " Network library = dbmssocn; network address = 127.0.0.1; " +
12 " Integrated Security = sspi; initial catalog = adventureworks " ;
13
14 Using (Sqlconnection connection = New Sqlconnection (connectstring ))
15 {
16 Connection. open ();
17
18 // Return some information about the server.
19 Console. writeline (
20 " Connectionstate = {0} \ ndatasource = {1} \ nserverversion = {2} " ,
21 Connection. State, connection. datasource,
22 Connection. serverversion );
23 }
24
25 Console. writeline ( " \ Npress any key to continue. " );
26 Console. readkey ();
27 }
28 }
29 }
30
Do this through SQL Server Configuration Manager by expanding the SQL server network configuration node and enabling TCP/IP in the protocols subnode. You will need to restart SQL server for the change to take effect.
To connect to SQL Server using an IP address, the TCP/IP network library must be used to connect to the SQL server. this is done by specifying the library in the connection string as either the attributeNetOrNetwork LibraryWith a valueDbmssocn. Specify the IP address usingData Source, server, address, ADDR, OrNetwork AddressParameter. The following connection string demonstrates using an IP address to specify the Data source:
"Network library = dbmssocn; network address = 127.0.0.1;" +
"Integrated Security = sspi; initial catalog = adventureworks";
In the example, the IP address 127.0.0.1 is the IP address for the local machine. This cocould also be specified (Local). To specify a SQL Server other than a local instance, specify the IP address of the computer on which SQL Server is installed.
Default instances of SQL Server listen on port 1433. named instances of SQL Server 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. if the SQL server is configured to listen on another port, specify the port number following the IP address specified byNetwork AddressAttribute separated by a comma as shown in the following snippet, which connects to a local SQL server listening on port 1450:
Network Address=(Local ),1450