What's the best method to check if SQL server exists or not?
I'm trying Microsoft.SqlServer.Management.Smo.Server.PingSqlServerVersion() and it works fine if server exists and available. But it kinda pretty slow, if there is no such a server.
Is there any fast enough method to check without even defining user credentials (only the server name), if a server exists?
What do you recommend to use?
You could just use TcpClient class to query the server and check if a specific port is open, could be something like this:
using System.Net;
using System.Net.Sockets;
public bool CheckServerAvailablity(string serverIPAddress, int port)
{
try
{
IPHostEntry ipHostEntry = Dns.Resolve(serverIPAddress);
IPAddress ipAddress = ipHostEntry.AddressList[0];
TcpClient TcpClient = new TcpClient();
TcpClient.Connect(ipAddress , port);
TcpClient.Close();
return true;
}
catch
{
return false;
}
}