Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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:
C#
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 :

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


2#
C#
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;
myProcess.Start();

myProcess.WaitForExit();
myProcess.Close();


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
Updated 18-Jan-12 22:39pm
v4

1 solution

Hi,

Check this link

thanks
-Amit.
 
Share this answer
 
Comments
iamgun10a 18-Jan-12 7:40am    
Not working (-u user -p**** PIEx \"<\" + "\"" + path + "\"") - and all combination ..
Same result.
AmitGajjar 18-Jan-12 8:09am    
use /< instead of <
iamgun10a 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 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 19-Jan-12 4:37am    
BAT file works fine :|
But still, WTF it dont works ?

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