使用vb.net备份和还原数据mysql

时间:2019-04-01 18:06:08

标签: mysql vb.net-2010 restore database-backups

我想使用vb.net 2017和xampp备份和还原数据库MySql。 仍然是初学者,如果有人有相关的源代码或教程,请提供帮助。

1 个答案:

答案 0 :(得分:0)

将此代码添加到函数中,切记导入所需的库

Dim timbackedup As String = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")
        Try
            Dim myconnection As New MySqlConnection(ConnectionString)
            If myconnection.State <> ConnectionState.Open Then
                myconnection.Open()
            End If

            Dim file2 As String = ""
            sfd.Filter = "SQL files | *.sql"
            sfd.FileName = "my filename " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".sql"
            Dim BackupFile As String

            If sfd.ShowDialog = DialogResult.OK Then
                 BackupFile = sfd.FileName
                Using sConnection As New MySqlConnection(ConnectionString)
                    Using sqlCommand As New MySqlCommand()
                        Using sqlBackup As New MySqlBackup(sqlCommand)
                            sqlCommand.Connection = sConnection
                            sConnection.Open()
                            sqlBackup.ExportToFile(BackupFile)

                            file2 = Path.GetDirectoryName(BackupFile)


                            MessageBox.Show("Backup has been created.", "Database Backup", MessageBoxButtons.OK, MessageBoxIcon.Information)


                            sConnection.Close()
                        End Using
                    End Using
                End Using
            Else
                MessageBox.Show("No backup file was created.", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            If myconnection.State = ConnectionState.Open Then
                myconnection.Close()
            End If
        Catch ex As Exception
            If ex.Message.Contains("MySQL hosts") Then
                MessageBox.Show("Server not found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If
        End Try


'RESTORE
Try
            Dim myconnection As New MySqlConnection(ConnectionString)
            If myconnection.State <> ConnectionState.Open Then
                myconnection.Open()
            End If 
            Dim RestoreFile As String
            Dim fileOpener As OpenFileDialog = New OpenFileDialog()
            fileOpener.Filter = "SQL files | *.sql"
            If fileOpener.ShowDialog() = Windows.Forms.DialogResult.OK Then
                 MsgBox("Click OK to continue")
                RestoreFile = fileOpener.FileName


                Using sConnection As New MySqlConnection(ConnectionString)
                    Using sqlCommand As New MySqlCommand()
                        Using sqlBackup As New MySqlBackup(sqlCommand)
                            sqlCommand.Connection = sConnection
                            sConnection.Open()
                            sqlBackup.ImportFromFile(RestoreFile)
                            deletefileaspath(RestoreFile)


                            MessageBox.Show("Restored.", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            sConnection.Close()
                        End Using
                    End Using
                End Using
            Else
                MessageBox.Show("No restore file was chosen.", "Database Restore", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            If myconnection.State = ConnectionState.Open Then
                myconnection.Close()
            End If
        Catch ex As Exception
            If ex.Message.Contains("MySQL hosts") Then
                MessageBox.Show("Server not found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If
        End Try