Check if server exists

Here is the problem. The user can enter the server name and connection string for the database. If the server is unavailable (wrong address, firewalls or any other problem), I want to know about it as quickly as possible.

If I use sqlConnection and try to connect to an insecure server, it takes a very long time (I think more than 1 minute!). This has nothing to do with the btw connection timeout, so setting this property will not help.

My idea is to try the ping server first, and if I get a response (which means the server is accessible from the application point), then proceed with sqlConnection. If there is no response from the response, the operation is aborted and the user is notified accordingly.

Is there a better way to do this? Any idea would be welcome.

I forgot to point out that I am using NHibernate, so I cannot use any specific MSSQL library. In addition, the target server may have Linux, not just Windows.

+4
source share
6 answers

This answer to the previous question has a decent approach , which is more elegant, since only ping using WMI will give you more information in general

+4
source

I think it's worth noting that the only really good way to verify the existence and accessibility of a shared database is to access it. The machine can be configured with a well-functioning database and, for example, refuse letters. Similarly, a machine can start a database instance and be configured to ignore / reject all WMI requests. If you can assume for your environment that this is not so (i.e. you know that all your companies will always respond to pings), you can continue; otherwise, you may just need to click "try connecting."

+1
source

You could use SqlDataSourceEnumerator to get a DataTable with all the available SQLServers, and then check if users are in the list. Something like that. See MSDN.

System.Data.DataTable table = VisibleServerList.GetVisibleServers(); public static class VisibleServerList { public static System.Data.DataTable GetVisibleServers() { System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; return instance.GetDataSources(); } } 
+1
source

When creating a connection to the server, set the connection timeout to about 2 or 3 seconds. Thus, it will help out much faster if, in fact, the server is not there.

The only drawback is that the server exists and it gets clogged, then you can get a false negative result. But this is rarely a problem.

0
source

In the end, I decided to use a hybrid approach. When the user clicks the "Connect" button, ping will be performed on the server user input. If the server does not have an answer (which can be detected very quickly), the user will receive a message that the server seems unavailable, and the question is if he wants to continue and try to connect to this server.

Basically, the user will decide that he will wait for a โ€œconnection attemptโ€.

0
source

You can simply use the TcpClient class to query the server and check if any port is open, maybe 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; } } 
0
source

Source: https://habr.com/ru/post/1303936/


All Articles