Dim scSvcs() As ServiceController = ServiceController.GetServices()
' Windowsサービスのサービス名を列挙する Dim sc As ServiceController = Nothing
For Each sc In scSvcs If sc.DisplayName = "SQL Server (SQLEXPRESS)" Then SqlServiceName = sc.ServiceName Exit For End If Next
Return SqlServiceName
End Function
Public Function StartSQL(myServiceName As String) Dim status As String Dim mySC As ServiceController
mySC = New ServiceController(myServiceName) Try status = mySC.Status.ToString Catch ex As Exception Debug.Print("Service not found. It is probably not installed. [exception=" & ex.Message & "]") Return False End End Try
If mySC.Status.Equals(ServiceControllerStatus.Stopped) Or mySC.Status.Equals(ServiceControllerStatus.StopPending) Then Try mySC.Start() mySC.WaitForStatus(ServiceControllerStatus.Running) Return True Catch ex As Exception Return False End Try End If