Click here to Skip to main content
Sign Up to vote bad
good
See more: C#MySQLProcess
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 :
 
1#
Process.Start(@"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe", ("-u user -p**** < \"" + path + "\""));
 
2#
            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 17-Jan-12 21:30pm
Edited 18-Jan-12 22:39pm


1 solution

Hi,
 
Check this link
 
thanks
-Amit.
  Permalink  
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 ?
amitgajjar - 19-Jan-12 23:29pm
no idea dear :) your work done is more important !!!!
iamgun10a - 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 - 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
Your Filters
Interested
Ignored
     
0 Christian Graus 514
1 Ron Beyer 296
2 OriginalGriff 248
3 samadhan_kshirsagar 229
4 Tadit Dash 213
0 Sergey Alexandrovich Kryukov 7,017
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 3,010


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