我可以用 smo 启动 sql server browser 服务吗?

发布于 2024-12-14 03:36:56 字数 107 浏览 3 评论 0原文

我需要从 VB.Net 程序枚举 SQL Server。如果 SQL Browser 服务未打开,则此操作会失败。默认安装似乎已禁用它。

我如何测试服务状态,如果需要,启用它并启动它?

I need to enumerate SQL Servers from a VB.Net program. This fails if the SQL Browser service is not on. The default installation seems to have it disabled.

How can i test for the service status and if required, enable it and start it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2024-12-21 03:36:56

您不一定需要 SMO。您可以使用 ServiceController 类。这是启动未运行的服务的简单示例:

' don't forget to reference the System.ServiceProcess and Imports System.Management DLLs 

imports System
imports System.ServiceProcess
imports System.Management

public module MyModule
Sub Main()
    Dim serviceName as String = "SQLBrowser"
    Dim sc As ServiceController = New ServiceController(serviceName)


    ' make sure start mode is automatic
    Dim path As String = "Win32_Service.Name='" & serviceName & "'"
    Dim p As New ManagementPath(path)
    Dim ManagementObj As New ManagementObject(p)
    Dim parameters As Object() = New Object(0) {}
    Dim value As String = "Automatic"
    parameters(0) = value
    ManagementObj.InvokeMethod("ChangeStartMode", parameters)

    if sc.Status = ServiceControllerStatus.Stopped or sc.Status = ServiceControllerStatus.Paused then
        Console.WriteLine(serviceName + " is starting.")
        sc.Start()
    else
        Console.WriteLine(serviceName + " is running.")
    end if
End Sub

end module

You don't necessarily need SMO. You can use the ServiceController class. Here's a simple example of starting a service that is not running:

' don't forget to reference the System.ServiceProcess and Imports System.Management DLLs 

imports System
imports System.ServiceProcess
imports System.Management

public module MyModule
Sub Main()
    Dim serviceName as String = "SQLBrowser"
    Dim sc As ServiceController = New ServiceController(serviceName)


    ' make sure start mode is automatic
    Dim path As String = "Win32_Service.Name='" & serviceName & "'"
    Dim p As New ManagementPath(path)
    Dim ManagementObj As New ManagementObject(p)
    Dim parameters As Object() = New Object(0) {}
    Dim value As String = "Automatic"
    parameters(0) = value
    ManagementObj.InvokeMethod("ChangeStartMode", parameters)

    if sc.Status = ServiceControllerStatus.Stopped or sc.Status = ServiceControllerStatus.Paused then
        Console.WriteLine(serviceName + " is starting.")
        sc.Start()
    else
        Console.WriteLine(serviceName + " is running.")
    end if
End Sub

end module

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文