StringBuilder commandinfo = new StringBuilder();
string directory = @"E:\dbSave.sql";//导出sql文件存储的路径
commandinfo.AppendFormat("mysqldump --quick --host=localhost --default-character-set=gbk --lock-tables --verbose --force --port=3306 --user=root --password=111111 test -r \"{0}\"", directory);//3306-端口号、111111-数据库等密码、test-我的数据库名称
String exportCommand = commandinfo.ToString();
//获取mysqldump.exe所在路径
String mysqldumpDirecroty = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = mysqldumpDirecroty;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;//是否显示CMD窗口
p.Start();
p.StandardInput.WriteLine(exportCommand);
p.StandardInput.WriteLine("exit");