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("資料庫已自動備份本地");
}
}
}
}
}