[英]How to take mysql backup or Restore with 'SPACE' in the given path C#
[英]mySQL backup and restore c#
该代码没有错误。 但是当我备份时,文件的大小仅为0kb或1kb。 当我恢复时,什么也没发生。 所有数据仍被删除。
备份代码:
string path;
path = "D:\\MySqlBackup"+ ".sql";
StreamWriter file = new StreamWriter(path);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName ="C:\\xampp\\mysql\\bin\\mysqldump.exe";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "--user=root --password=database --database=hotelreservationandbillingsystem < D:\\MySqlBackup.sql";
psi.UseShellExecute = false;
Process process = Process.Start(psi);
string output;
output = process.StandardOutput.ReadToEnd();
file.WriteLine(output);
process.WaitForExit();
file.Close();
process.Close();
MessageBox.Show("Back Up Successfully!","Saved",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
恢复代码:
string path;
path = "D:\\MySqlBackup.sql";
StreamReader file = new StreamReader(path);
string input = file.ReadToEnd();
file.Close();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "C:\\xampp\\mysql\\bin\\mysqldump.exe";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.Arguments = "--user=root --password=database --database=hotelreservationandbillingsystem < D:\\MySqlBackup.sql";
psi.UseShellExecute = false;
Process process = Process.Start(psi);
process.StandardInput.WriteLine(input);
process.StandardInput.Close();
process.WaitForExit();
process.Close();
MessageBox.Show("Restored Successfully!", "Restored", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
为什么不尝试采用更简单的方法来获取可用的备份和还原选项MySqlBackup.NET-C#的MySQL备份解决方案
备份的代码在哪里
string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ExportToFile(file);
conn.Close();
}
}
}
并还原
string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ImportFromFile(file);
conn.Close();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.