0

应用截图:

在此处输入图像描述

申请代码:

Dim myController As New System.ServiceProcess.ServiceController("SQL Server (SQLEXPRESS)")
Sub ed()
    If txtNum.Text = "Started" Then
        btnStart.Enabled = False
        btnStop.Enabled = True
    ElseIf txtNum.Text = "Stopped" Then
        btnStop.Enabled = False
        btnStart.Enabled = True
    End If
End Sub
Sub Na1()
    If myController.Status = ServiceProcess.ServiceControllerStatus.Running Then
        txtNum.Text = "Started"
    ElseIf myController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
        txtNum.Text = "Stopped"
    End If
End Sub
Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Na1()
    ed()
End Sub
Private Sub Test_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
    Me.Refresh() '////////This one is not required
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    ProgressBar1.Value = 0
    Try
        myController.Start()
    Catch ex As Exception
        MsgBox(ex.Message) 
    Finally
        ProgressBar1.Value = 100
        Na1()'//////////////////////////////////////////////Edit Code here
    End Try
End Sub

Private Sub txtNum_TextChanged(sender As Object, e As EventArgs) Handles txtNum.TextChanged
    ed()
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    ProgressBar1.Value = 0
    Try
        myController.Stop()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        ProgressBar1.Value = 100
        Na1() '//////////////////////////////////////////////Edit Code here
    End Try
End Sub

您需要停止 SQL 服务以复制 .mdf 文件,并且从“服务”手动停止/启动服务非常烦人,因此我尝试使用 Vb.Net 编码使其更容易...

4

1 回答 1

0

编辑以下代码,

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
ProgressBar1.Value = 0
Try
    myController.Start()
Catch ex As Exception
    MsgBox(ex.Message) 
Finally
    ProgressBar1.Value = 100
    Na1()'//////////////////////////////////////////////Edit Code here
End Try
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    ProgressBar1.Value = 0
    Try
        myController.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        ProgressBar1.Value = 100
        myController.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)'//Add
        Na1()
    End Try
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
ProgressBar1.Value = 0
Try
    myController.Stop()
Catch ex As Exception
    MsgBox(ex.Message)
Finally
    ProgressBar1.Value = 100
    Na1() '//////////////////////////////////////////////Edit Code here
End Try
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    ProgressBar1.Value = 0
    Try
        myController.Stop()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        ProgressBar1.Value = 100
        myController.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped) '//Add
        Na1()
    End Try
End Sub

一切都完成了!

于 2016-07-07T09:13:24.843 回答