我所做的是检查服务状态(MSSQL$idealsql)并等待服务运行。
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
一旦服务上线,这将返回正确的结果,
但是
SQLServer需要一段时间才能在服务启动后完全运行。
更新
此过程仅在应用程序启动时使用。
伪代码。
检查数据库是否存在
正确->执行结构等所需的任何更新
继续执行主程序
结束更新
然后,我需要检查数据库是否存在(如果不存在,则动态创建数据库),这是通过
系统数据库
,并查找我们的数据库是否存在,
但是
再一次,所有的数据库实际上线需要一点时间。
我还找到了一些额外的代码来检查TempDB的创建情况,并获取它的创建日期,以确定服务何时上线。
SELECT crdate AS StartTime FROM sys.sysdatabases WHERE name = 'tempdb'
根据EventVwr,TempDB是第一个上线的数据库。
是否有合适的方法确定SQLServer完全联机,并且所有数据库都已启动?
虽然显示的代码是VB.Net,但我并不太关心这种语言,更重要的是,这是确定数据库服务器是否完全在线的正确方法。