thongkorn โพสต์ 2023-5-2 13:23:59

[VB.NET] โค้ดการ Start / Stop Service ของ MySQL Server

โค้ด VB.NET (2010) ในการ Start / Stop Service ของ MySQL Server (Localhost) ... สำหรับแอดมินอยู่สายงาน Win App ไม่ได้ใช้ Web ก็เลยไม่จำเป็นต้องลง Web Server ของ XAMPP แต่ใช้ MySQL Workbench ในการบริหารจัดการก็พอล่ะ ในการเขียนโค้ดนี้ขึ้นมาเพราะแอดมินจะต้องตามไปเปิดปิด Service ของ MySQL อยู่ตลอดเวลา คือเปิดเมื่อใช้งาน และจะปิดเมื่อเลิกใช้ แต่ใช้เป็น Win App มันจะสะดวกกว่าครับ ...

Services.msc
http://www.g2gsoft.com/webboard/images/VBNet/mysqlservice.jpg

Add References ... ServiceProcess
http://www.g2gsoft.com/webboard/images/VBNet/mysqlserviceref.jpg

Status Running ...
http://www.g2gsoft.com/webboard/images/VBNet/mysqlstart.jpg

Status Stopping ...
http://www.g2gsoft.com/webboard/images/VBNet/mysqlstop.jpg
มาดูโค้ดกันเถอะ ...
'// Don't forget manually add a reference .NET to System.ServiceProcess.dll
Imports System.ServiceProcess

'// ServiceController Class
'// https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicecontroller?view=dotnet-plat-ext-7.0

Public Class frmMySqlService
    '// Find the Machine Name or "." for this PC (Local).
    Private MachineName As String = My.Computer.Name    '// "."
    '// Specific MySQL Local Instance name.
    Private SqlName As String = "MySQL57"
    '// ServiceController(MySQL Instance Name, Machine Name)
    Dim sc As New ServiceController

    Private Sub btnAction_Click(sender As System.Object, e As System.EventArgs) Handles btnAction.Click
      Try
            Select Case ServiceStatus()
                Case "Running"
                  sc.Stop()
                  sc.WaitForStatus(ServiceControllerStatus.Stopped)
                  MessageBox.Show("Stop " & SqlName & " Servers.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
                  btnAction.Text = "Start Service"
                Case "Stopped", "StopPending"
                  sc.Start()
                  sc.WaitForStatus(ServiceControllerStatus.Running)
                  MessageBox.Show("Start " & SqlName & " Servers.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
                  btnAction.Text = "Stop Service"
            End Select
            lblStatus.Text = "Status: " & ServiceStatus()
      Catch ex As Exception
            MessageBox.Show(ex.Message)
      End Try
    End Sub

    '// S T A R T ... H E R E
    Private Sub frmMySqlService_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      lblStatus.Text = ""
      Try
            '// Check the status of the service.
            Select Case ServiceStatus()
                Case "Running"
                  btnAction.Text = "Stop Service"
                Case "Stopped", "StopPending"
                  btnAction.Text = "Start Service"
            End Select
            lblStatus.Text = "Status: " & sc.Status.ToString
      Catch ex As Exception
            MessageBox.Show(ex.Message)
            End
      End Try
    End Sub

    Function ServiceStatus() As String
      '// ServiceControllerStatus Meanings.
      '1 = Stopped – The Service is not running.
      '2 = StartPending – The Service is starting.
      '3 = StopPending – The Service is stopping.
      '4 = Running – The Service is running.
      '5 = ContinuePending – The Service continue is pending.
      '6 = PausePending – The Service pause is pending.
      '7 = Paused – The service is paused.

      '// ServiceController(MySQL Instance Name, Machine Name)
      sc = New ServiceProcess.ServiceController(SqlName, MachineName)
      '// Return string.
      Return sc.Status.ToString
    End Function

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
      Me.Close()
    End Sub

    Private Sub frmMySqlService_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      GC.SuppressFinalize(Me)
      End
    End Sub

End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...
หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] โค้ดการ Start / Stop Service ของ MySQL Server