Easiest tool to backup and restore Mysql database from windows.

Introduction

It is the basic, easy to use MySQL database Backup/Restore utility.

Background

It is basically developed for non programmers. So, anyone can use it. The simplest tool for the job.

Using the code

Backup method takes two arguments, i.e, path to save the backup file and connection string.

//  Backup MySql database
private void MySQLBackup(string path, string constring)
{
    string file = path + txt_database.Text + “_” + DateTime.Now.ToString(“yyyy-MM-dd_hh-mm-ss”) + “.sql”;
    using (MySqlConnection conn = new MySqlConnection(constring))
    {
       using (MySqlCommand cmd = new MySqlCommand())
       {
          using (MySqlBackup mb = new MySqlBackup(cmd))
          {
             cmd.Connection = conn;
             conn.Open();
             mb.ExportToFile(file);
             conn.Close();
          }
       }
    }
}

Now to the restore method with the same two arguments

// Restore MySQL(.sql) file to the database
private void RestoreSQL(string file, string constring)
{
   using (MySqlConnection conn = new MySqlConnection(constring))
   {
      using (MySqlCommand cmd = new MySqlCommand())
      {
         using (MySqlBackup mb = new MySqlBackup(cmd))
         {
            cmd.Connection = conn;
            conn.Open();
            mb.ImportFromFile(file);
            conn.Close();
         }
      }
   }
}