1:引用dll
MySql.Data.dll, MySqlbackup.dll
2:建一个数据连接静态类
public static class mysql
{
public static string constr = "database=test;Password=密码;user ID=root;server=ip地址";
public static MySqlConnection conn = new MySqlConnection(constr);
}
3:定时器启动:
winform加载时
timer1.Interval = 1000; //代表一秒运行一次
timer1.Enabled = true; //启动
4:建winform窗体 timer定时器控件
备份代码
private void timer1_Tick(object sender, EventArgs e)
{
if (booql)
{
booql = false;
if (DateTime.Now.Hour == 10 && DateTime.Now.Minute == 00) //时间10点
{
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("数据库已自动备份本地");
}
}
}
}
}