什么是检查SQL服务器的可用性最快的方法是什么? [英] What's the fastest method to check SQL server availability?
问题描述
什么是检查如果SQL Server存在与否的最好方法?
What's the best method to check if SQL server exists or not?
我想Microsoft.SqlServer.Management.Smo.Server.PingSqlServerVersion()
,如果服务器存在并且可正常工作。
但是有点很慢,如果没有这样的服务器。
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?
你有什么推荐使用?
推荐答案
您可以只使用 TcpClient的类查询服务器并检查是否有特定的端口是开放的,可能是这样的:
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;
}
}
这篇关于什么是检查SQL服务器的可用性最快的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!