Chapter 12.161.5 connect to the database through an IP address

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.