0

I'm using the following code to test if SQL Server is running, and I'm facing a strange behavior, the result is true only if I set a breakpoint under that statement, otherwise it will return false. My timeout value is set to 120.

public static bool IsServerRunning()
{
    string[] serverDetails= serverName.Split(',');
    string fullServerName = serverDetails[0];
    string[] stringSeparators = new string[] { "\\" };
    string[] fullServerNameDetails = fullServerName.Split(stringSeparators, StringSplitOptions.None);

    string serverIpAdress = fullServerNameDetails[0];
    int serverPort = int.Parse(serverDetails[1]);
    int timeout = GetRemoteServerTimeout();

    var result = false;
    try
    {
        using ( var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, Sockets.ProtocolType.Tcp) )
        {
            IAsyncResult asyncResult = socket.BeginConnect(serverIpAdress, serverPort, null, null);
            result = asyncResult.AsyncWaitHandle.WaitOne(timeout, true);
            socket.Close();
        }
        return result;
    }
    catch ( Exception myException )
    {
        
        return false;
    }
}

UPDATE:

I've found a working solution, however I'm not sure if it the most safe/reliable way to implement it.

 private static bool IsServerRunning()
    {
        try
        {
            using (var sqlConnection = new SqlConnection(GetConnectionString()))
            {
                sqlConnection.Open();
                return true;
            }
        }
        catch (Exception ex)
        {
            log.Error("Error message for connection failed " + ex.Message);
            return false; 
        }
Elena2020
  • 43
  • 1
  • 5
  • Does this answer your question? [How to Efficently Test Whether an SQL Server Instance is Running in C#](https://stackoverflow.com/questions/9312882/how-to-efficently-test-whether-an-sql-server-instance-is-running-in-c-sharp) and [Detecting if SQL server is running](https://stackoverflow.com/questions/22979/detecting-if-sql-server-is-running) and [In C# what is the best way to determine if a database is up and running?](https://stackoverflow.com/questions/927567/in-c-sharp-what-is-the-best-way-to-determine-if-a-database-is-up-and-running) –  Jan 27 '21 at 14:18
  • This code looks like [Check that Sql Server exists prior to connection](https://www.codeproject.com/Articles/612751/Check-that-Sql-Server-exists-prior-to-connection). –  Jan 27 '21 at 14:21
  • Yes, is the same code sample. – Elena2020 Jan 27 '21 at 14:25
  • If you only want to check if SQL Server is running on the local machine, not distant or a particular instance, you can search [its process name](https://stackoverflow.com/questions/262280/how-can-i-know-if-a-process-is-running) "`sqlservr`". If the service is started you will get it, else nothing. –  Jan 27 '21 at 14:25
  • 1
    Actually I'm trying to check if a remote sql server is running. – Elena2020 Jan 27 '21 at 14:27
  • Does this answer your question? [How can I check if a specific process is running on a remote PC/Server?](https://stackoverflow.com/questions/34271920/how-can-i-check-if-a-specific-process-is-running-on-a-remote-pc-server) and [Check status of services that run in a remote computer using C#](https://stackoverflow.com/questions/1335065/check-status-of-services-that-run-in-a-remote-computer-using-c-sharp) –  Jan 27 '21 at 14:33

1 Answers1

0

I am not an expert in c# but it looks like using IAsyncResult you return the result before the result actually get set. why dont you use this more simplistic solution posted by @RameshDurai here

Tch
  • 1,055
  • 5
  • 11