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