Backing up Database in MySQL using C#
You can use MySqlBackup.NET as alternative to MySqlDump
Documentation:
http://www.codeproject.com/Articles/256466/MySqlBackup-NET-MySQL-Backup-Solution-for-Csharp-V
https://github.com/MySqlBackupNET/MySqlBackup.Net
Sample codes:
Backup a MySQL database
using MySql.Data.MySqlClient;
then the code,
private void Backup(){ string constring = "server=localhost;user=root;pwd=qwerty;database=test;"; string file = "C:\\backup.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(); } } }}
Restore a MySQL database
private void Restore(){ string constring = "server=localhost;user=root;pwd=qwerty;database=test;"; string file = "C:\\backup.sql"; 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(); } } }}
Update:
I am one of the author of this library.
I have try that code but problem is occure in before run.exeption is -An unhandled exception of type 'System.IO.FileLoadException' occurred in System.Windows.Forms.dll
Additional information: Could not load file or assembly 'MySql.Data, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I believe you have to mention the user, pwd, db name and the target path..
string path = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > " + txtBoxDBName.Text + @".sql";
backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
restore:# mysql -u root -p[root_password] [database_name] < dumpfilename.sql
http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/