使用C#在MySQL中备份数据库

我创建了Winforms以备份我的数据库。 然后,当我运行我的程序时,它给出了一个未处理的Win32Exception。 “系统找不到指定的文件”虽然该文件已存在并导致该exception。

这是关于我的问题的代码

using System.Diagnostics; private void btnProceed_Click(object sender, EventArgs e) { path = @"D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > D:\C#\Client\Salesmate - EMC\SalesMate\Backup\" + maskeTxtBoxDBFile.Text + @""; Process p = new Process(); p.StartInfo.FileName = path; p.Start(); } 

您可以使用MySqlBackup.NET替代MySqlDump
文档:
http://www.codeproject.com/Articles/256466/MySqlBackup-NET-MySQL-Backup-Solution-for-Csharp-V
https://github.com/MySqlBackupNET/MySqlBackup.Net

示例代码:

备份MySQL数据库

 private void Backup() { 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(); } } } } 

恢复MySQL数据库

 private void Restore() { 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(); } } } } 

更新:
我是这个图书馆的作者之一。

我已经尝试了该代码但是在运行之前出现了问题。 exeption是 – System.Windows.Forms.dll中出现类型为“System.IO.FileLoadException”的未处理exception

附加信息:无法加载文件或程序集’MySql.Data,Version = 6.5.4.0,Culture = neutral,PublicKeyToken = c5687fc88969c44d’或其依赖项之一。 定位的程序集的清单定义与程序集引用不匹配。 (HRESULTexception:0x80131040)

我相信你必须提到用户,密码,数据库名称和目标路径..

 string path = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > " + txtBoxDBName.Text + @".sql"; 

backup:#mysqldump -u root -p [root_password] [database_name]> dumpfilename.sql

restore:#mysql -u root -p [root_password] [database_name]

http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/

  • 不要将整个调用推入“path =”,你应该使用“Arguments”来指定参数,如名称所示。 如果库检查是否存在被调用文件(您的整个路径),它应该找不到它!
  • 你确定路径是正确的吗? 您应该使用注册表找到MySQL服务器路径,而不是硬编码路径,或者如果您不容易,可以从命令行将其作为参数传递或从表单中指定(设置页面)。
  • 您可能错过了凭据:-u应该用于用户名(即使我使用–user),-p应该用于密码(即使我使用–password)。 为什么你传递“txtBoxDB Name .Text”作为密码?!
  • 也许您的目标路径无效:它包含空格,如果您使用空格,则应使用引号。
  • txtBoxDBName.Text(?password?)包含什么? 太空了吗? 如果是,它不起作用。
  • + @""最后存在完全没用,它不插入任何引号。

更正引号的代码的正确版本是: path = @"""D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe"" -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > ""D:\C#\Client\Salesmate - EMC\SalesMate\Backup\" + maskeTxtBoxDBFile.Text + @"""";

为了更好的可读性: path = $@"""D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe"" -u {txtBoxDBUsername.Text} -p {txtBoxDBName.Text} > ""D:\C#\Client\Salesmate - EMC\SalesMate\Backup{maskeTxtBoxDBFile.Text}""";

 ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = @"C:\\xampp\mysql\bin\mysql.exe"; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = false; psi.CreateNoWindow = true; psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}<{4}", "root", "password", "localhost", "your_dbname", "your_.sql_file"); psi.UseShellExecute = false; Process process = Process.Start(psi); process.StandardInput.WriteLine(input); process.StandardInput.Close(); process.WaitForExit(); process.Close(); 

这个对我有用,只要你备份了.sql文件就可以试试

你可以尝试这个。

  public void BackUpData(string file) { using (MySqlConnection con = new MySqlConnection { ConnectionString = config }) { using (MySqlCommand cmd = new MySqlCommand { Connection = con }) { using (MySqlBackup mb = new MySqlBackup { Command = cmd }) { try { con.Open(); try { mb.ExportToFile(file); } catch(MySqlException ex) { msgErr(ex.Message + " sql query error."); return; } } catch(MySqlException ex) { msgErr(ex.Message + " connection error."); return; } } } } }