3

I have created the mysql database dump file(utf8) successfully with this:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Application.StartupPath + "\\mysqldump.exe";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "-r D:\\backup.sql --user=root --password=1234 --opt databasename";
psi.UseShellExecute = false;

Process process = Process.Start(psi);
process.WaitForExit();
process.Close();

I am able to restore the utf8 dump file into database successfully by using windows CMD with this single line command:

mysql.exe -u root --password=1234 databasename < d:\backup.sql

but, I failed to execute the command in C#. Could you please advice me where is gone wrong. Thanks you very much. Below are my commands:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Application.StartupPath + "\\mysql.exe";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = false;
psi.Arguments = "--user=root --password=1234 databasename < D:\\backup.sql";
psi.UseShellExecute = true;

Process process = Process.Start(psi);
process.WaitForExit();
process.Close();
mjb
  • 7,649
  • 8
  • 44
  • 60
  • 1
    *Sidenote:* You want to use [`Path.Combine`](http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx) to join paths. – Bobby Aug 12 '11 at 07:02
  • I'm not sure, but I think you can't use pipes and output-redirections when starting a process. As far as I know those are features of the command-prompt. – Bobby Aug 12 '11 at 07:03
  • 1
    shouldn't it be like -u --password ..... Also you may also look into this http://www.dotnetspider.com/resources/19537-MySQL-Database-Backup-Restore-from-C-NET.aspx – V4Vendetta Aug 12 '11 at 07:08

2 Answers2

0

Are you sure that your C# exe is executed in the same directory where MySQL is installed? If you are running from your commandline at c: the FileName would be C:\mysqldump.exe. I doubt it's lying there. If mysqldump.exe is in the path you'd probably won't need to append the Application.StartupPath or any other path...

Sascha
  • 10,231
  • 4
  • 41
  • 65
  • 1
    Yes, my app exe is not the same directory where MySQL installed. The original location of mysql.exe and mysqldump.exe is located at C:\Program Files\MySQL\MySQL Server 5.1\bin. I copied the two exe file (mysql.exe and mysqldump.exe) into my app directory. mysqldump.exe works succesfully in my app directory. For mysql.exe, its still under study and finding ways to make it work. – mjb Aug 12 '11 at 11:16
0

You might want to leave out the database name in the restore. since you created the dump file with a DB name it doesn't need and may not like the database name you provided. I know it ran at the command line, so that may not be it, but the syntax i saw was psi.Arguments = "--user=root --password=1234 < D:\backup.sql";

If the database already exists were you want to restore it, you may need to use mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

I also noticed you used psi.RedirectStandardOutput = false; in the restore where it was true in the backup. you are not using the output so they should likely both be false.

if that all fails, if you can include the error you are getting on the restore that may be helpful