Click here to Skip to main content
11,648,094 members (63,656 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# MySQL Process
I try to backup and restore MySQL database from C# using process.

First BackUP:
the command to backup database is :
-u USER -pPASS --databases DATABASE -r > path/to/file.sql
Here is my C# code:
Process.Start(@"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe", ("-u user -p***** --databases DB -r \"" + path + "\"";
Look at the difference, in my code is not ">", becuse it wont work with it. If I put this char to code, it just show me usage of mysqldump in win cmd. Without it, this code work good.

Now i want to restore database.
the command to restore database is :
-u USER -pPASS DATABASE < path/to/file.sql
Here is my c# codes :

Process.Start(@"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe", ("-u user -p**** < \"" + path + "\""));

Process myProcess = new Process();
            myProcess.StartInfo.FileName = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe";
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.WorkingDirectory = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\";
            myProcess.StartInfo.Arguments = "-u user -p**** PIEx < + "\"" + path + "\"";
            myProcess.StartInfo.RedirectStandardInput = false;
            myProcess.StartInfo.RedirectStandardOutput = false;

Both solutions dont work. I think it is becuse of this char ">", the same problem like backup, but in this case, removing it dont work. The result is same, it write down mysql usages in cmd console.

If I execute this commands manually in cmd (move to mysql bin, then run mysql with arguments), it work fine. I try redirect output/input/error to stream readers/writers - not work. Try to UseShellExecute = true - not work. Nothing works.

Help me guys. Thx
Posted 17-Jan-12 21:30pm
Edited 18-Jan-12 22:39pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Check this link

iamgun10a at 18-Jan-12 7:40am
Not working (-u user -p**** PIEx \"<\" + "\"" + path + "\"") - and all combination ..
Same result.
amitgajjar at 18-Jan-12 8:09am
use /< instead of <
iamgun10a at 18-Jan-12 9:50am
What should it do ? This dont work ... If you mean \<, so < is not escape character so dont work too.
amitgajjar at 18-Jan-12 23:13pm
if this can't be done then create one .bat file. create it and execute it from your code.
iamgun10a at 19-Jan-12 4:37am
BAT file works fine :|
But still, WTF it dont works ?
amitgajjar at 19-Jan-12 23:29pm
no idea dear :) your work done is more important !!!!
iamgun10a at 20-Jan-12 3:04am
Not at all :|
Its business app for customer company and now everyone can restore database with bat file - thats not good. And I can not create bat in code becuse of the same problem : redirection chars :/
amitgajjar at 20-Jan-12 3:31am
you can create bat file from the code and execute it. what you have problem in creating file ? you can create it like normal txt file.

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

  Print Answers RSS
0 500
1 F-ES Sitecore 370
2 DamithSL 290
3 CPallini 290
4 OriginalGriff 277
0 OriginalGriff 1,342
1 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 845

Advertise | Privacy | Mobile
Web02 | 2.8.150804.4 | Last Updated 31 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100