How to determine if SQL Server service is fully running and all databases are online

With some slower machines running our application (which is located in the Windows startup folder) when the machine is restarted and autologized, the SQL Server service is still in a running state when my application is running. (SqlServer 2005)

What I did to counter this was to check the status of the service (MSSQL $ idealsql) and wait for the service to start.

Public Function isServiceRunning() As Boolean
    Dim theServices() As System.ServiceProcess.ServiceController
    Dim theservice As System.ServiceProcess.ServiceController
    theServices = System.ServiceProcess.ServiceController.GetServices
    Dim running As Boolean

    For Each theservice In theServices
        If String.Equals(theservice.ServiceName, mServiceName, StringComparison.OrdinalIgnoreCase) Then
            running = (theservice.Status = ServiceProcess.ServiceControllerStatus.Running)
            Exit For
        End If
    Next
    Return running
End Function

This returns the correct result after the service arrives on the network, but SQLServer takes a little time before it is fully operational after the service starts.


.
.
( )

 false →
 true → , ..

( , ), , sysdatabases, , , , , .

TempDB , , , .

SELECT crdate AS StartTime FROM sys.sysdatabases WHERE name = 'tempdb'

EventVwr, TempDB - , .

, SQLServer , ?

- VB.Net, , , .

+3
1

( 10 ) -,

SELECT 1 FROM Some Table

.

(1 NULL) .NET DBOnline DBOffline .

DMV ( SQL Server 2005): , SQL Server

USE master
GO 

SELECT TOP 1        
    sample_ms AS Millisecond_Since_Start
FROM        
    sys.dm_io_virtual_file_stats(DB_ID(), NULL)
+1

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


All Articles