本文介绍了检查服务是否在VB.Net中运行时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个例程,可以在尝试运行任何SQL命令之前检查SQL Server是否在网络系统上运行.在检查过程中,我得到2个不同的结果:1.用户没有问题,一切正常. 2.对于其他用户,检查控制器状态将返回异常无法在计算机上打开MSSQLSERVER服务...",我再次只是在检查服务的状态.我不是试图停止或开始;只是检查一下.有什么建议?这是代码:
I have a routine that checks to see if the SQL server is running on a network system before I attempt to run any SQL commands. During the check I get 2 different results: 1. Users have no issue, all works fine. 2.for other users the check of the controller status returns an exception "Cannot open MSSQLSERVER service on computer..." Again I am just checking the status of the service. I am not attempting to stop or start; just check. Any suggestions? Here is the code:
Public Sub CheckForSQL(ByVal Server As String)
Try
Dim controller As New System.ServiceProcess.ServiceController("MSSQLSERVER")
controller.MachineName = Server
If controller.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Running) AndAlso controller.CanStop Then
'All good continue on
SQLExists = True
Return
ElseIf controller.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Stopped) Then
controller.Start()
SQLExists = True
Return
Else
MsgBox("SQL Controller Status: " & controller.Status)
SQLExists = False
Return
End If
Catch ex As Exception
MsgBox(ex.Message)
SQLExists = False
Return
End Try
End Sub
推荐答案
这篇关于检查服务是否在VB.Net中运行时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!