Cannot restore mysql dump file with mysql.exe in C#

4k views Asked by At

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();
2

There are 2 answers

1
Sascha On

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...

0
Thundersquirrel On

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