c#利用定时器自动备份数据库(mysql)
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("数据库已自动备份本地");
}
}
}
}
}
上一篇: 为什么现实中禁止朝天开枪?血的教训告诉你