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

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

[VB.NET] การทำ Backup และ Restore MySQL DataBase Server (Local/Remote)

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

308

กระทู้

499

โพสต์

6027

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6027



โค้ด VB.NET ในการทำ Backup และ Restore MySQL Server จะใช้หลักการดังนี้ คือ ทำการเชื่อมต่อเข้าไปยัง MySQL Server และแสดงผลฐานข้อมูลทุกตัวออกมา จากนั้นก็ทำการเลือกฐานข้อมูลที่อยู่ใน ComboBox ก่อน แล้วค่อยเลือกทำการ Backup หรือ Restore ... โค้ดชุดนี้แอดมินทดสอบแล้วทำงานได้ทั้งแบบ Local Server และ Remote Server ...

Add References ... (ไฟล์ DLL อยู่ใน ZIP)


มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Imports MySql.Data.MySqlClient
  2. Imports System.IO

  3. Public Class frmMySQL
  4.     '// Declare variable one time but use many times.
  5.     Dim Conn As MySqlConnection
  6.     Dim Cmd As MySqlCommand
  7.     Dim DR As MySqlDataReader
  8.     Dim DA As MySqlDataAdapter
  9.     'Dim DS As DataSet
  10.     'Dim DT As DataTable
  11.     Dim strSQL As String '// Major SQL
  12.     'Dim strStmt As String    '// Minor SQL
  13.     '//
  14.     Dim MyPathBackup As String = GetPath(Application.StartupPath + "Backup")
  15.     ' / --------------------------------------------------------------------------------
  16.     '// MySQL Server DBMS Connection Test with VB.NET (2010).
  17.     Public Function ConnectMySQL(ByVal DNS As String, ByVal UID As String, PWD As String, ByRef cmb As ComboBox, Optional ByVal DB As String = "") As Boolean
  18.         Dim strCon As String = String.Empty
  19.         '// Connect DataBase Only
  20.         If DB = "" Then
  21.             strCon = _
  22.                 " Server=" & DNS & "; " & _
  23.                 " User ID=" & UID & "; " & _
  24.                 " Password=" & PWD & "; " & _
  25.                 " Port=3306; " & _
  26.                 " CharSet=utf8; " & _
  27.                 " Connect Timeout=120000; " & _
  28.                 " Pooling = True; " & _
  29.                 " Persist Security Info=True; " & _
  30.                 " Connection Reset=False; "
  31.         Else
  32.             strCon = _
  33.                 " Server=" & DNS & "; " & _
  34.                 " User ID=" & UID & "; " & _
  35.                 " Database=" & DB & "; " & _
  36.                 " Password=" & PWD & "; " & _
  37.                 " Port=3306; " & _
  38.                 " CharSet=utf8; " & _
  39.                 " Connect Timeout=120000; " & _
  40.                 " Pooling = True; " & _
  41.                 " Persist Security Info=True; " & _
  42.                 " Connection Reset=False; "
  43.         End If
  44.         Try
  45.             Conn = New MySqlConnection
  46.             Conn.ConnectionString = strCon
  47.             '// Connect MySQL Server and Listing All DataBase.
  48.             If DB = "" Then
  49.                 Conn.Open()
  50.                 Cmd = New MySqlCommand("SHOW DATABASES", Conn)
  51.                 DR = Cmd.ExecuteReader()
  52.                 If DR.HasRows Then
  53.                     cmb.Items.Clear()
  54.                     While DR.Read()
  55.                         cmb.Items.Add(DR("Database").ToString)
  56.                     End While
  57.                     cmb.SelectedIndex = 0
  58.                 End If
  59.                 DR.Close()
  60.                 Cmd.Dispose()
  61.                 Conn.Close()
  62.                 Conn.Dispose()
  63.                 MessageBox.Show("Connect MySQL Server Complete.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  64.                 Return True

  65.                 '// Operation for Backup & Restore MySQL DataBase.
  66.             Else
  67.                 Conn.Open()
  68.                 Conn.Close()
  69.                 Conn.Dispose()
  70.                 Return True
  71.             End If

  72.         Catch ex As Exception
  73.             MessageBox.Show(ex.Message, "Report Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  74.             Return False
  75.         End Try
  76.     End Function

  77.     Private Sub frmMySQL_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  78.         Me.Dispose()
  79.         GC.SuppressFinalize(Me)
  80.         Application.Exit()
  81.     End Sub

  82.     '// START HERE.
  83.     Private Sub frmMySQL_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  84.         Try
  85.             '// Create Backup Folder if doesn't exist.
  86.             If (Not System.IO.Directory.Exists(GetPath(Application.StartupPath) & "Backup")) Then System.IO.Directory.CreateDirectory(GetPath(Application.StartupPath) & "Backup")
  87.             btnConnect.Enabled = True
  88.             btnBackup.Enabled = False
  89.             btnRestore.Enabled = False
  90.             cmbDataBase.Enabled = False
  91.         Catch ex As Exception
  92.             MessageBox.Show(ex.Message)
  93.         End Try
  94.     End Sub

  95.     '// Connect to MySQL Server and List all Databases.
  96.     Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
  97.         Try
  98.             '// Parameters: SERVER_NAME, DB_USERNAME, DB_PASSWORD, ComboBox Control Name, DB_NAME (Default = "")
  99.             If Not ConnectMySQL(txtServer.Text.Trim, txtDBUserName.Text.Trim, txtDBPassword.Text.Trim, cmbDataBase) Then Return
  100.             btnConnect.Enabled = False
  101.             btnBackup.Enabled = True
  102.             btnRestore.Enabled = True
  103.             cmbDataBase.Enabled = True
  104.         Catch ex As Exception
  105.             MessageBox.Show(ex.Message)
  106.         End Try
  107.     End Sub

  108.     '// Backup DataBase.
  109.     Private Sub btnBackup_Click(sender As System.Object, e As System.EventArgs) Handles btnBackup.Click
  110.         Dim DBFile As String = String.Empty
  111.         Dim dlgSaveFile = New SaveFileDialog
  112.         Try
  113.             With dlgSaveFile
  114.                 .Title = "Save MySQL DataBase"
  115.                 .InitialDirectory = MyPathBackup
  116.                 .Filter = "SQL Dump File (*.sql)|*.sql"
  117.                 .FileName = cmbDataBase.Text + "-" + DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".sql"
  118.                 .RestoreDirectory = True
  119.             End With
  120.             If dlgSaveFile.ShowDialog = DialogResult.OK Then
  121.                 '// Parameters: SERVER_NAME, DB_USERNAME, DB_PASSWORD, ComboBox Control Name, DB_NAME
  122.                 If ConnectMySQL(txtServer.Text.Trim, txtDBUserName.Text.Trim, txtDBPassword.Text.Trim, cmbDataBase, cmbDataBase.Text) Then
  123.                     DBFile = dlgSaveFile.FileName
  124.                     Using Cmd = New MySqlCommand()
  125.                         Using mb As MySqlBackup = New MySqlBackup(Cmd)
  126.                             Cmd.Connection = Conn
  127.                             Conn.Open()
  128.                             mb.ExportToFile(DBFile)
  129.                             Conn.Close()
  130.                         End Using
  131.                     End Using
  132.                     MessageBox.Show("Backup Your MySQL DataBase Successfully!", "Backup MySQL DataBase", MessageBoxButtons.OK, MessageBoxIcon.Information)
  133.                 End If
  134.             End If
  135.         Catch ex As Exception
  136.             MessageBox.Show(ex.Message)
  137.         End Try

  138.     End Sub

  139.     '// Restore DataBase.
  140.     Private Sub btnRestore_Click(sender As System.Object, e As System.EventArgs) Handles btnRestore.Click
  141.         Dim DBFile As String = String.Empty
  142.         Dim dlgOpenFile = New OpenFileDialog
  143.         Try
  144.             With dlgOpenFile
  145.                 .Title = "Open MySQL DataBase"
  146.                 .InitialDirectory = MyPathBackup
  147.                 .Filter = "SQL Dump File (*.sql)|*.sql"
  148.                 .RestoreDirectory = True
  149.             End With
  150.             If dlgOpenFile.ShowDialog = DialogResult.OK Then
  151.                 Dim Result As Byte = MessageBox.Show("Are you sure to RESTORE into DataBase Name: " & cmbDataBase.Text & "?", "Confirm to Restore", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
  152.                 If Result = DialogResult.Yes Then
  153.                     '// Parameters: SERVER_NAME, DB_USERNAME, DB_PASSWORD, ComboBox Control Name, DB_NAME
  154.                     If ConnectMySQL(txtServer.Text.Trim, txtDBUserName.Text.Trim, txtDBPassword.Text.Trim, cmbDataBase, cmbDataBase.Text) Then
  155.                         DBFile = dlgOpenFile.FileName
  156.                         Using Cmd = New MySqlCommand()
  157.                             Using mb As MySqlBackup = New MySqlBackup(Cmd)
  158.                                 Cmd.Connection = Conn
  159.                                 Conn.Open()
  160.                                 mb.ImportFromFile(DBFile)
  161.                                 Conn.Close()
  162.                             End Using
  163.                         End Using
  164.                         MessageBox.Show("Restore your MySQL DataBase Successfully!", "Restore MySQL DataBase", MessageBoxButtons.OK, MessageBoxIcon.Information)
  165.                     End If
  166.                 End If
  167.             End If

  168.         Catch ex As Exception
  169.             MessageBox.Show(ex.Message)
  170.         End Try
  171.     End Sub

  172.     ' / --------------------------------------------------------------------------------
  173.     ' / Get my project path
  174.     ' / AppPath = C:\My Project\bin\debug
  175.     ' / Replace "\bin\debug" with ""
  176.     ' / Return : C:\My Project\
  177.     Function GetPath(AppPath As String) As String
  178.         '/ MessageBox.Show(AppPath);
  179.         AppPath = AppPath.ToLower()
  180.         '/ Return Value
  181.         GetPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  182.         '// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
  183.         If Microsoft.VisualBasic.Right(GetPath, 1) <> Chr(92) Then GetPath = GetPath & Chr(92)
  184.     End Function

  185.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  186.         Me.Close()
  187.     End Sub
  188. End Class
คัดลอกไปที่คลิปบอร์ด

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

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

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

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

33

โพสต์

372

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
372
โพสต์ 2020-8-30 13:35:41 | ดูโพสต์ทั้งหมด

ขอบคุณคับ

0

กระทู้

6

โพสต์

222

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
222
โพสต์ 2020-8-30 21:19:46 | ดูโพสต์ทั้งหมด

ขอบพระคุณมากเลยครับผม

5

กระทู้

21

โพสต์

165

เครดิต

Member

Rank: 2

เครดิต
165
โพสต์ 2020-9-28 19:50:28 | ดูโพสต์ทั้งหมด

ขอบคุณครับ อาจารย์

2

กระทู้

6

โพสต์

48

เครดิต

Newbie

Rank: 1

เครดิต
48
โพสต์ 2023-1-11 09:51:54 | ดูโพสต์ทั้งหมด

มันขึ้นเหมือนหา ตัว MySqlBackup ไม่เจอครับ

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

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

x

308

กระทู้

499

โพสต์

6027

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6027
 เจ้าของ| โพสต์ 2023-1-11 14:14:57 | ดูโพสต์ทั้งหมด

ARABAN_BRK110 ตอบกลับเมื่อ 2023-1-11 09:51
มันขึ้นเหมือนหา ตัว MySqlBackup ไม่เจอครับ

Add References ไฟล์ DLL ที่จำเป็นต้องใช้งานเข้ามาด้วยครับ
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

2

กระทู้

6

โพสต์

48

เครดิต

Newbie

Rank: 1

เครดิต
48
โพสต์ 2023-1-11 17:32:23 | ดูโพสต์ทั้งหมด

thongkorn ตอบกลับเมื่อ 2023-1-11 14:14
Add References ไฟล์ DLL ที่จำเป็นต้องใช้งานเข้ามาด้วยครับ

นอกจาก
MySqlBackup.dll
MySql.Data.dll

เราจะรู้ได้ยังไงว่า  References  ตัวไหนที่ขาดไปอีกครับ

308

กระทู้

499

โพสต์

6027

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6027
 เจ้าของ| โพสต์ 2023-1-11 22:41:57 | ดูโพสต์ทั้งหมด

ARABAN_BRK110 ตอบกลับเมื่อ 2023-1-11 17:32
นอกจาก
MySqlBackup.dll
MySql.Data.dll

หากเราพัฒนาโปรแกรมเอง จำเป็นต้องศึกษาว่าต้องใช้ตัวไหนบ้าง หากกลัวลืมก็จะใช้การเรียก Import เข้ามาเอาไว้ หรือใช้การเขียน Comment อธิบายสั้นๆเอาไว้ครับ หากเวลาเราย้ายโปรเจคโค้ดต้นฉบับไปใช้กับเครื่องอื่น (หรือเกิดการดาวน์โหลดโปรเจค) บางทีมันไม่ Add Reference ให้แบบอัตโนมัติ กรณีนี้ให้ไปดูที่ Project Properties เพื่อเช็คว่าไฟล์ไหนที่มันหายไปบ้างครับ
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

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

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

GMT+7, 2024-4-19 11:59 , Processed in 0.392424 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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