Click here to Skip to main content
15,878,230 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am doing a windows application in that i used MySQL as back end.I want to take the backup of Database by writing code in If any body knows please send me that code. I googled but no use. I found one code but but its coming for 1st table only. I wan the whole Database.
This is the code which i used...

  DateTime backupTime = DateTime.Now;
  int year = backupTime.Year;
  int month = backupTime.Month;
  int day = backupTime.Day;
  int hour = backupTime.Hour;
  int minute = backupTime.Minute;
  int second = backupTime.Second;
  int ms = backupTime.Millisecond;

  String tmestr = backupTime.ToString();
  tmestr = "D:\\" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + ".sql";
  StreamWriter file = new StreamWriter(tmestr);
  ProcessStartInfo proc = new ProcessStartInfo();
  string cmd = string.Format(@"-u{0} -p{1} -h{2} {3} ", "root", "sa", "localhost", "employee", "backup.sql");
  proc.FileName = @"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump";
  proc.RedirectStandardInput = false;
  proc.RedirectStandardOutput = true;
  proc.Arguments = cmd;//"-u root -p smartdb > testdb.sql";
  proc.UseShellExecute = false;
  Process p = Process.Start(proc);
  string res;
  res = p.StandardOutput.ReadToEnd();
  MessageBox.Show("Backup Done");
catch (IOException ex)
  MessageBox.Show("Disk full or other IO error , unable to backup!");

But its coming for the 1st table only...
I want the whole database.

Thanks in advance......
Updated 22-Dec-10 9:25am
Marc A. Brown 22-Dec-10 15:25pm    
Added <pre> tags.
Henry Minute 23-Dec-10 15:51pm    
Then try one of the others. That's the great thing about search engines, they usually give you lots to choose from. When I goggled there was one hit (3rd or 4th on my list) that was hosted on SourceForge. It didn't use batch files at all. Have a look at the sources for some of the others and I'm sure that by using ideas from several, you will be able to achieve your aim.
Henry Minute 6-Jun-12 7:30am    
As your problem was over 18 months ago I assume that you have solved it by now. However, if you are still interested in the subject chirag.jdk has just posted a link to a useful looking article at the bottom of my original reply (judging from the title anyway, I haven't checked it out :)). Take a look.

1 solution

I can only assume that you didn't google very hard, or used a poor search phrase.

I just tried, using c# mysql backup and the first few hits that I looked at had code samples.
Share this answer
B Gopal 23-Dec-10 15:37pm    
i sawed in my 1st hit tooo..
but i am not getting how to pass parameters for that function "ExecuteBatchFile()"
I need some hlp in that
chirag.jdk 6-Jun-12 7:09am
Henry Minute 6-Jun-12 7:34am    
Judging from the title of your link that might be useful.
Two problems though:
1. I was not looking for this information so you should have added it to the comments section of the original question, not to my reply. :-)
2. The original question was over 18 months ago so if the OP hasn't solved it by now, you are probably wasting your time.

I have notified the OP about your link, so there is no need for you to do anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900