What should I use instead of ping?

In my Windows Forms application (at startup), I use the ping command to verify that both the Internet connection and my SQL Server are alive.

Is there any “better” way-command that I should use to accomplish the above task in the .NET framework 2.0?

+3
source share
6 answers

Just make a small simple query on your SQL server to see if the connection is working. Beyond that, why not just a reasonably handled exception if you come across a connection or fail in any other way?

+7
source

Ping can only be used to verify that packets can be transported to the destination, but is not suitable for verifying the health of a real server.

If your server is configured to provide SNMP, it would be better to select SNMP information to see if your server is running.

However, SNMP programming is not an easy task. Go if you really need benefits.

+2
source

:

private bool IsInternetAvailable()
        {
            bool ret = false;

            try
            {
                HttpWebRequest req = (HttpWebRequest)
            HttpWebRequest.Create("http://www.yourremoteserver.com/");
                HttpWebResponse res 
            = (HttpWebResponse)req.GetResponse();
                if (res.StatusCode == HttpStatusCode.OK)
                {
                    ret = true;
                }
                else
                {
                    ret = false;
                }
                res.Close();
            }
            catch
            {
                ret = false;
            }

            return ret;
        }

:

SQL Server " ".

+1

MS-SQL Server :

// Setup a timer to call as needed
public bool IsSqlConnectionOpen(string connectionString)
{
    bool open = false;
    using (var connection = new SqlConnection(connectionString))
    {
        try
        {
            connection.Open();
            open = true;
            connection.Close();
        }
        catch (SqlException e)
        {
            // log e.ToString()
        }
    }
    return open;
}

, SQL Server.

: MSDN

, .

SqlConnection, , . , , - .

, Pooling=false , .

+1

. System.Net . HttpWebRequest -, .

0

, . , , . : . , . , - - .

Another point, I saw this in combination, where the question about querying the database server is the time on the server. those. select GetDate ()

This does two things: first you know that the server and the network are working if you get a response. Secondly, if you have critical time operations, you can measure the skew between the client computer and the database server. Sometimes it’s important to know.

0
source

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


All Articles