Recent backup and attachment capabilities for MYSQ databases through C #
Because MySQL has similar backup and additional CMD commands, but has not been used, today, under the practice, feel very efficient, more efficient than their own writing. Below I list the backup and additional function functions that C # calls MySQL.
1. Back up the MySQL database
Defines a string straddress = string. Format ("Mysqldump--host={0}--default-character-set=utf8--lock-tables--routines--force--port=3306--user={1}-- PASSWORD={2}--quick ", the server name of the connection, user name, password);
String strdb= the name of the database you need to back up;
This.mysqlpath = "C:\\Program Files\\mysql\\mysql Server 5.5\\bin";
if (!string. IsNullOrEmpty (STRDB))
{
SfD. Filter = "Database file |*.sql";
SfD. FilterIndex = 0;
SfD. Restoredirectory = true;
SfD. FileName = "backup-" + strDB + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". sql";
if (SFD. ShowDialog () = = DialogResult.OK)
{
String FilePath = sfd. FileName;
string cmd = this.straddress + strDB + ">" + filePath;
string result = Runcmd (M_mysqlpath, CMD);
if (result. Trim () = = "")
{
Show ("Database backup succeeded!") "," hint ",);
}
Else
{
Show (result, "hint");
}
}
}
Main operating functions
Private String Runcmd (String strpath, String strcmd)
{
process P = new process ();
p.startinfo.filename = "cmd.exe";
p.startinfo.workingdirectory = strpath;
P.startinfo.useshellexecute = false;
p.startinfo.redirectstandardinput = true;
p.startinfo.redirectstandardoutput = true;
p.startinfo.redirectstandarderror = true;
P.startinfo.createnowindow = true;
P.start ();
P.standardinput.writeline (Strcmd);
P.standardinput.writeline ("Exit");
return p.StandardError.ReadToEnd ();
}
Perform a backup of the selected database.
Backup and attach MySQL database through C #