1. Simple example
//backup restore MySQL
Public Static voidTestone () {Process P=NewProcess (); p.StartInfo.FileName="Cmd.exe"; P.startinfo.useshellexecute=false; P.startinfo.redirectstandardinput=true; P.startinfo.redirectstandardoutput=true; P.startinfo.redirectstandarderror=true; P.startinfo.createnowindow=true; //Specify the bin directory of the MySQL programP.startinfo.workingdirectory =@"E:\mysql-5.6.26-winx64\bin";
P.start (); /************* * Backup command **************/
//Simple Format//string strSQL = "mysqldump--quick--host=localhost-u root-p123 test > D:\\test_20151227110010.sql";
/*command to specify the format*/
//String command = "mysqldump--quick--host=localhost--default-character-set=gb2312--lock-tables--verbose--force
--port= Port number--user= user name--password= password database name-r backup to address "; //string strSQL = "mysqldump--quick--host=localhost--default-character-set=gb2312--lock-tables--verbose--force- -port=3306--user=root--password=123 test-r d:\\one.sql ";
/************* * Restore command **************/
//Simple Format//string strSQL = "MySQL--host=localhost-u root-p123 test < D:\\test_20151227110010.sql";
/** RESTORE command format **/
//string s = "MySQL--host=localhost--default-character-set=gbk--port= port number--user= user name--password= password database name < restore file path ";
stringstrSQL ="MySQL--host=localhost--port=3306--user=root--password=123 Test < D:\\test_20151227110010.sql"; P.standardinput.writeline (strSQL+"&exit"); P.standardinput.autoflush=true; /** * * Execute command, no verifiable results returned * * * **/
//display Mode 1//StreamReader reader = p.standardoutput; //String line = reader.
ReadLine (); //While (!reader. Endofstream)//{
//Console.WriteLine (line); //Line = reader.
ReadLine (); //}
//display Mode 2
stringresult =P.standardoutput.readtoend ();
Console.WriteLine (result); //returns the warning result--warning:using a password on the command line interface can is insecure.
stringRESULT2 =p.StandardError.ReadToEnd ();
Console.WriteLine (RESULT2); //display Mode 3
Showvalue (P);
p.WaitForExit ();
P.close (); }
Private Static Async voidshowvalue (Process p) {stringresult =awaitP.standardoutput.readtoendasync ();
Console.WriteLine (result); }
C # program calls CMD Execute command-mysql backup restore