问题描述
publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}#region备份privatevoidbutton1_Click(objectsender,EventArgse){if(MessageBox.Show("是否进行数据库备份?","标题",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK){try{StringBuildersbcommand=newStringBuilder();StringBuildersbfileName=newStringBuilder();sbfileName.AppendFormat("{0}","数据备份文件",System.Globalization.DateTimeFormatInfo.InvariantInfo);StringfileName=sbfileName.ToString();SaveFileDialogsaveFileDialog=newSaveFileDialog();saveFileDialog.AddExtension=false;saveFileDialog.CheckFileExists=false;saveFileDialog.CheckPathExists=false;saveFileDialog.FileName=fileName;//SaveFileDialogsaveFileDialog=newSaveFileDialog();saveFileDialog.InitialDirectory=Environment.GetFolderPath(Environment.SpecialFolder.Personal);saveFileDialog.Filter="文本文件(*.sql)|*.sql|所有文件(*.*)|*.*";if(saveFileDialog.ShowDialog(this)==DialogResult.OK){Stringdirectory=saveFileDialog.FileName;sbcommand.AppendFormat("MySQLdump--quick--host=localhost--default-character-set=gbk--lock-tables--verbose--force--port=3306--user=root--password=rootqwe-r"{0}"",directory);//主要是这里,命令在DOS里运行可以,这里就不行。Stringcommand=sbcommand.ToString();MessageBox.Show(command);//Stringcommand=sbcommand.ToString();//获取mysqldump.exe所在路径StringappDirecroty=System.Windows.Forms.Application.StartupPath+"\";Cmd.StartCmd(appDirecroty,command);MessageBox.Show(@"数据库已成功备份到"+directory+"文件中","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);}}catch(Exception){MessageBox.Show("数据库备份失败!");}}