简体   繁体   中英

Test Connectivity of SQL Server from remote machine using C#

I need to test the connectivity of SQL Server with my remote machine,

I have SQL server name not IP address.

How can I check this connectivity. How will I know on which port the communication enabled.

Well you must know on which port the Sql server services are listening on remote server. Default port is 1433. If you want to test if a port is open on a server or not then you can try Telnet ping.

open command prompt:

C:\> Telnet <YourServername> <portno.>

If it's open then a screen will go empty immediately or return an error.

You need to try to connect, there is no way find on which port someone is listening without directly asking, hey, is there anyone on this port?

Here is the way to do so from C#:

TcpClient tc = null;
try
{
    tc = new TcpClient("MyAddress", MyPort);
    // port is open
} 
catch(SocketException se) 
{
    // port is not open, or host is not reachable
}
finally
{
   tc.Close();
}

You need to know the IP address or host name of the server and the TCP port, too. The instance name alone won't help, as they are not advertised on the network. If you have the server's name or IP and the SQL Server instance name, you can simply try to connect using an SqlConnection with an appropriate connection string. If you can connect, everything's fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM