欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-10 23:49:36
1:引用dll MySql.Data.dll, MySqlbackup.dll 2:建一个数据连接静态类 public static class mysql{public static string constr = "database=test;Password=密码;user ID=root;s ......

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

}
}
}
}
}