1

I try to get backup to my database. My code is

private void button1_Click(object sender, EventArgs e)
{
    string Coonstring = "datasource=localhost;port=3306;username=****;password=****;Charset=utf8";
    MySqlConnection cn= new MySqlConnection(Coonstring);
    MySqlCommand cmd;
    SaveFileDialog sf = new SaveFileDialog();
    sf.Filter = "Backup Files (*.Bak) |*.bak";
    if(sf.ShowDialog()==DialogResult.OK)
    {
        cmd = new MySqlCommand("Backup Database project To Disk='" + sf.FileName + "'", cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        MessageBox.Show("Done");
        cn.Close();
    }
}

but when i run it i get error

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Backup Database project To Disk='C:\Users\tariq emad\Desktop\1\tt.bak'' at line 1).

So, any one can help me to fix this error.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tariq
  • 43
  • 7
  • 1
    Is `Backup Database project to Disk='blah.bak'` an actual valid MySQL command? – mellamokb Apr 16 '14 at 18:12
  • (blah) Is the File name . and (.bak) is the type of save it's mean this file backup file – tariq Apr 16 '14 at 18:15
  • 1
    @mellamokb I think it is a [valid command](http://stackoverflow.com/questions/3741846/query-for-backup-a-database-at-another-location-in-file-system) in SQL Server, but I am not sure about MySQL. – jaredk Apr 16 '14 at 18:17
  • [Backing up Database in MySQL using C#](http://stackoverflow.com/questions/12311492/backing-up-database-in-mysql-using-c-sharp) – 001 Apr 16 '14 at 18:18
  • @jaredk yes i think the issue in MYSQL command. So what is the right command that i can using it mysql – tariq Apr 16 '14 at 18:20

1 Answers1

0

In a cmd-box you need to execute mysqldump like this

mysqldump --user=... --Password=... dbname > dump.sql

Carsten Hellweg
  • 214
  • 1
  • 3
  • You mean like this cmd = new MySqlCommand(" mysqldump --user=... --Password=... dbname > dump.sql, cn); – tariq Apr 16 '14 at 18:28
  • there is á mysqldump.exe somewhere, that needs to be called from a cmd-box, not from code – Carsten Hellweg Apr 16 '14 at 18:30
  • Can Some one explain to me more. Cuz i'm new in this :) – tariq Apr 16 '14 at 18:32
  • i guess, that you use a windows machine... you have a mysqlserver installed somewhere. Inside the bin dir of the mysql, there are several exe files. One is called mysqldump! You need to execute that program for a dump of the dB. – Carsten Hellweg Apr 16 '14 at 18:33
  • The Command i posted is normally used for a backup of mysql. Goto Start, execute and Type cmd. The a Black dosbox should open. Locate the mysql Dir and Look for a dir Named bin using windows-explorer. Inside the dosbox Type "cd and then the Command above. Substitute the ... With Password, username and dbname with project... Then a file called dump.sql should appear with create table statements and Insert statements – Carsten Hellweg Apr 16 '14 at 18:36
  • Carsten Hellweg , I have Form with button it's name ( Save ) i want when i press on this button new window ask me where i want to save the .bak and ask me to give name . Then when i press ok the (file name.bak) save to my computer. So how i can do this using (mysqldump) – tariq Apr 16 '14 at 18:50
  • i dont know, how c# actually handles that kind of stuff, but in other languages, there is a command like "exec" or "System.exec", which calls other .exe files and returns the output. you may need something like that for c#. if you dont use "> dump.sql", then the output of mysqldump.exe will not be sent to the file dump.sql but to the output ... so if you call the programm from c# like this "c:\PATH\TO\MYSQL\bin\mysqldump --user=YOUR_USER --pass=YOUR_PASS DBNANE" with an exec command, that returns the output in a string, you may create your "save" button – Carsten Hellweg Apr 17 '14 at 13:04