当前位置: 移动技术网 > IT编程>开发语言>.net > c#利用定时器自动备份数据库(mysql)

c#利用定时器自动备份数据库(mysql)

2019年05月10日  | 移动技术网IT编程  | 我要评论

王雨葭,嵌入式开发,女尸体解剖图

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("数据库已自动备份本地");

}
}
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网