Test Connectivity of SQL Server from remote machine using C#

4nkexdtk  于 2023-06-04  发布在  SQL Server
关注(0)|答案(3)|浏览(149)

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.

gg0vcinb

gg0vcinb1#

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();
}
vfwfrxfs

vfwfrxfs2#

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.

vktxenjb

vktxenjb3#

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.

相关问题