Backup restore initialization for C # MySQL Database

Source: Internet
Author: User

Perform the CREATE DATABASE operation

This. Getexecute (G_con, "CREATE database if not exists newdb");

This.sqladdress = "-H" + IP + "-u" + User + "-P" + Password + "newdb";

Backup of the database

private void Btn_dump_click (object sender, EventArgs e)

{

using (SaveFileDialog SFD = new SaveFileDialog ())

{

SfD. Filter = "Database file |*.sql";

SfD. FilterIndex = 0;

SfD. Restoredirectory = true;

SfD. FileName = "BackUp" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". sql";

if (SFD. ShowDialog () = = System.Windows.Forms.DialogResult.OK)

{

String FilePath = sfd. FileName;

string cmd = "mysqldump" + sqladdress + "> \" "+ FilePath +" \ "";

string result = Runcmd (cmd);

if (result. Trim () = = "")

{

MessageBox.Show ("Database backup succeeded!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

Else

{

MessageBox.Show (Result, "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

}

}

Database Restore//RESTORE Database

private void Btn_import_click (object sender, EventArgs e)

{

if (this.tb_Path.Text.Trim () = = "")

{

MessageBox.Show ("Please select files to recover!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);

Return

}

This. Getexecute (G_con, "CREATE database if not exists clothes");

String filePath = This.tb_Path.Text.Trim ();

string cmd = "MySQL" + sqladdress + "< \" "+ FilePath +" \ ""; string result = Runcmd (cmd);

if (result. Trim () = = "")

{

MessageBox.Show ("Database recovery succeeded!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

Else

{

MessageBox.Show (Result, "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

Command-line Operations

private string Runcmd (String command)

{

Cases

Process Process P = new process ();

p.StartInfo.FileName = "cmd.exe"; Determine the program name

P.startinfo.arguments = "/C" + command; Determine the program command line

P.startinfo.useshellexecute = false; Use of the shell

P.startinfo.redirectstandardinput = true; REDIRECT Input

P.startinfo.redirectstandardoutput = true; REDIRECT Output

P.startinfo.redirectstandarderror = true; REDIRECT Output error

P.startinfo.createnowindow = true; Setting the Display window

P.start (); 00

P.standardinput.writeline (command); You can also enter the command in the line in this way

P.standardinput.writeline ("Exit"); Add exit or the next line of code

p.WaitForExit ();

P.close ();

return P.standardoutput.readtoend ();

Output out stream get command line result fruit

return p.StandardError.ReadToEnd ();

}

Backup restore initialization for C # MySQL Database

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.