当前位置:   article > 正文

c#+mysql+表备份,使用C#备份数据库MySQL中

using (mysqlbackup mb = new mysqlbackup(cmd))

I created a Winforms in order to backup my Database. Then When I run my program it gives an Win32Exception was unhandled. "The system cannot find the file specified" Although the file is already existed and resulted to that exception.

Here is my code regarding my problem

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();

}

解决方案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();

}

}

}

}

Restore a MySQL database

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();

}

}

}

}

Update:

I am one of the author of this library.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/711585
推荐阅读
相关标签
  

闽ICP备14008679号