C # program calls CMD Execute command-mysql backup restore

Source: Internet
Author: User
Tags mysql backup

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.