ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 633|ตอบกลับ: 0

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

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6052

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6052

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

Services.msc


Add References ... ServiceProcess


Status Running ...


Status Stopping ...

มาดูโค้ดกันเถอะ ...
  1. '// Don't forget manually add a reference .NET to System.ServiceProcess.dll
  2. Imports System.ServiceProcess

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

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

  12.     Private Sub btnAction_Click(sender As System.Object, e As System.EventArgs) Handles btnAction.Click
  13.         Try
  14.             Select Case ServiceStatus()
  15.                 Case "Running"
  16.                     sc.Stop()
  17.                     sc.WaitForStatus(ServiceControllerStatus.Stopped)
  18.                     MessageBox.Show("Stop " & SqlName & " Servers.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  19.                     btnAction.Text = "Start Service"
  20.                 Case "Stopped", "StopPending"
  21.                     sc.Start()
  22.                     sc.WaitForStatus(ServiceControllerStatus.Running)
  23.                     MessageBox.Show("Start " & SqlName & " Servers.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  24.                     btnAction.Text = "Stop Service"
  25.             End Select
  26.             lblStatus.Text = "Status: " & ServiceStatus()
  27.         Catch ex As Exception
  28.             MessageBox.Show(ex.Message)
  29.         End Try
  30.     End Sub

  31.     '// S T A R T ... H E R E
  32.     Private Sub frmMySqlService_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  33.         lblStatus.Text = ""
  34.         Try
  35.             '// Check the status of the service.
  36.             Select Case ServiceStatus()
  37.                 Case "Running"
  38.                     btnAction.Text = "Stop Service"
  39.                 Case "Stopped", "StopPending"
  40.                     btnAction.Text = "Start Service"
  41.             End Select
  42.             lblStatus.Text = "Status: " & sc.Status.ToString
  43.         Catch ex As Exception
  44.             MessageBox.Show(ex.Message)
  45.             End
  46.         End Try
  47.     End Sub

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

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

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

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

  70. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-26 06:50 , Processed in 0.184360 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้