2

i want the user of my vb application to be able to backup and restore the database (MySQL) onto a storage medium. my problem is that i dont want to specify 'c:\ in the code because i want the application to be able to locate the dumb file whether it is created on drive c or not. below is the code i used but when i installed it on another machine, it had its windows and program files on D:. it turns out that i have to check the drive letter of every machine, change it in the code before i publish the application to allow backup which i dont want to do that. i want it do be universal. thus whether the dump file is on driver C, G or whatever. any help. below is the code i used. Dim cmd As String

Private Sub cmdBackup_Click()
    Screen.MousePointer = vbHourglass
    DoEvents

    cmd = Chr(34) & "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump" & Chr(34) & " -uroot -psecretpswd --routines --comments db_name > c:\MyBackup.sql"
    Call execCommand(cmd)

    Screen.MousePointer = vbDefault
    MsgBox "done"
End Sub

3 Answers 3

2

There is a complied DLL called MySqlBackup.NET. Actually it is an alternative to MySqlDump.

Features

  • Export/Import Table's Structures & Rows
  • Export/Import Stored Procedures, Functions, Triggers, Events, Views
  • Custom Tables and Rows Export.
  • Able to apply encryption to the process.
  • Export BLOB and save as files.
  • Gather SQL Syntax errors during Import process.
  • Export/Import will report progress. Enable the usage of progress bar.
  • Able to execute in Synchronous or Asynchronous mode.
  • Export/Import To/From Zip File.

For more info, see the link below,

Edited: Code Examples Added

Backup a MySql Database

Dim con As String = "server=localhost;user=root;pwd=1234;database=test;"
Dim file As String = "C:\backup.sql"
Dim mb As New MySqlBackup(con)
mb.ExportInfo.FileName = file
mb.Export()

Restore a MySql Database

Dim con As String = "server=localhost;user=root;pwd=1234;database=test;"
Dim file As String = "C:\backup.sql"
Dim mb As New MySqlBackup(con)
mb.ImportInfo.FileName = file
mb.Import()
Sign up to request clarification or add additional context in comments.

Comments

0

Usually this commands are built using parameters external to the application, not hard coding path to MySqlDump, Database Name and path to destination folder.

Your code should be changed to something like this

Private Sub cmdBackup_Click()
    Screen.MousePointer = vbHourglass
    DoEvents
    Dim mySqlDumpCmd = ConfigurationManager.AppSettings("PathToMySqlDump")
    Dim dbName = ConfigurationManager.AppSettings("DatabaseToBackup")
    Dim destPath = ConfigurationManager.AppSettings("DestinationPath")
    cmd = Chr(34) & mySqlDumpCmd & Chr(34) & " -uroot -psecretpswd --routines --comments " +
          dbName & " > " & destPath
    Call execCommand(cmd)
    Screen.MousePointer = vbDefault
    MsgBox "done"
End Sub

and your application.config file contains these values

<?xml version="1.0"?>
<configuration>
    <configSections>
    .......

    <appSettings>
    <add key="PathToMySqlDump" value="C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump.exe"/>
    <add key="DatabaseToBackup" value="db_name"/>
    <add key="DestinationPath" value="C:\MyBackup.sql"/>
    </appSettings>
    .......

In this way you read the key information from the config file of your application. If the need arises you can easily change the information used by the command without touching anything in your application

Comments

0

Use this code. It works for me.

I had such a problem and then found this article

"http://www.experts-exchange.com/Programming/Languages/.NET/Q_27155602.html"

Example was in C#. I manually converted it into vb.net and add converting into 'utf8'.

   Imports System.Text
   Public Class Form1
       Dim OutputStream As System.IO.StreamWriter
        Sub OnDataReceived1(ByVal Sender As Object, ByVal e As    System.Diagnostics.DataReceivedEventArgs)
          If e.Data IsNot Nothing Then
          Dim text As String = e.Data
          Dim bytes As Byte() = Encoding.Default.GetBytes(text)
          text = Encoding.UTF8.GetString(bytes)
          OutputStream.WriteLine(text)
         End If
    End Sub

    Sub CreateBackup()
        Dim mysqldumpPath As String = "d:\mysqldump.exe"
        Dim host As String = "localhost"
        Dim user As String = "root"
        Dim pswd As String = "Yourpwd"
        Dim dbnm As String = "BaseName"
        Dim cmd As String = String.Format("-h{0} -u{1} -p{2} {3}", host, user, pswd, dbnm)
        Dim filePath As String = "d:\backup\fieName.sql"
        OutputStream = New System.IO.StreamWriter(filePath, False, System.Text.Encoding.UTF8)

        Dim startInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()
        startInfo.FileName = mysqldumpPath
        startInfo.Arguments = cmd

        startInfo.RedirectStandardError = True
        startInfo.RedirectStandardInput = False
        startInfo.RedirectStandardOutput = True 
        startInfo.UseShellExecute = False
        startInfo.CreateNoWindow = True
        startInfo.ErrorDialog = False

        Dim proc As System.Diagnostics.Process = New    System.Diagnostics.Process()
        proc.StartInfo = startInfo
        AddHandler proc.OutputDataReceived, AddressOf OnDataReceived1
        proc.Start()
        proc.BeginOutputReadLine()
        proc.WaitForExit()

        OutputStream.Flush()
        OutputStream.Close()
        proc.Close()
     End Sub

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As      System.EventArgs) Handles MyBase.Load
       CreateBackup()
       End Sub
      End Class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.