# mysql数据库备份还原
引用dll
MySql.Data.dll, MySqlbackup.dll
建静态类
public static class mysql
{
public static string constr = "database=test;
Password=密码;
user ID=root;
server=ip地址";
public static MySqlConnection conn = new MySqlConnection(constr);
}
备份代码
DialogResult result = MessageBox.Show("备份路径默认在当前程序下", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
string time1 = System.DateTime.Now.ToString("d").Replace("/", "-");
string file = ".//mysql/" + time1 + "_test.sql";
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = mysql.conn;
mysql.conn.Open();
mb.ExportToFile(file);
mysql.conn.Close();
MessageBox.Show("已备份");
}
}
} else
{
return;
}
还原代码
string file = textBox1.Text;
if (file == "")
{
MessageBox.Show("不能为空");
return;
}
DialogResult result = MessageBox.Show("确定还原吗?", "还原", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = mysql.conn;
mysql. conn.Open();
mb.ImportFromFile(file);
mysql. conn.Close();
MessageBox.Show("已还原");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} else
{
return;
}
推荐阅读
- Docker应用:容器间通信与Mariadb数据库主从复制
- gitlab|gitlab 通过备份还原 admin/runner 500 Internal Server Error
- 数据库设计与优化
- 数据库总结语句
- py连接mysql
- 2019-01-18Mysql中主机名的问题
- MySql数据库备份与恢复
- mysql|InnoDB数据页结构
- 数据库|SQL行转列方式优化查询性能实践
- mysql中视图事务索引与权限管理