mysql定时数据备份工具(c#)
此博文的出处 为 http://blog.csdn.net/zhujunxxxxx/article/details/40124773zhujunxxxxx@163.com ,如有问题请联系作者 为了确保数据的安全,我们往往要对数据进行备份。但是为了减少我们的工作量,我写了一个简单的数据备份工具,实现定时备份数据库。 其
此博文的出处 为 http://blog.csdn.net/zhujunxxxxx/article/details/40124773zhujunxxxxx@163.com,如有问题请联系作者
为了确保数据的安全,我们往往要对数据进行备份。但是为了减少我们的工作量,我写了一个简单的数据备份工具,实现定时备份数据库。
其实程序很简单,数据备份的工作就是几个mysql的命令而已。
先看看程序的运行界面
可以看到界面是十分的简单的
我们使用的是命令行来进行数据备份,所以我们的程序需要一个能够执行命令行的函数
/// <summary> /// 执行Cmd命令 /// </summary> /// <param name="workingDirectory">要启动的进程的目录 /// <param name="command">要执行的命令 public static void StartCmd(String workingDirectory, String command) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.WorkingDirectory = workingDirectory; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(command); p.StandardInput.WriteLine("exit"); }
接下来是一个备份数据库的函数
public void bakup_db() { try { //String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址"; //构建执行的命令 StringBuilder sbcommand = new StringBuilder(); StringBuilder sbfileName = new StringBuilder(); sbfileName.AppendFormat("{0}", DateTime.Now.ToShortDateString()).Replace("-", "").Replace(":", "").Replace(" ", "").Replace("/", ""); String fileName = sbfileName.ToString(); String directory = bakpath + fileName+".bak"; sbcommand.AppendFormat("mysqldump --quick --host=localhost --default-character-set=utf8 --lock-tables --verbose --force --port=3306 --user={0} --password={1} {2} -r \"{3}\"", uname, upass, dbname, directory); String command = sbcommand.ToString(); //获取mysqldump.exe所在路径 //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\"; StartCmd(appDirecroty, command); } catch (Exception ex) { } }
还原数据库
public void recovery_db() { //string s = "mysql --port=端口号 --user=用户名 --password=密码 数据库名<br> 为了实现定时备份,我们使用的是一个Timer组件,来实现定时的数据备份 <pre class="brush:php;toolbar:false">private void timer1_Tick(object sender, EventArgs e) { int h = DateTime.Now.Hour; if (h == hour) { bakup_db(); } }
给出完整的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; namespace MysqlBak { public partial class Form1 : Form { //备份文件的路径 public String bakpath="d:\\db_bak\\"; public String appDirecroty = @"C:\Program Files (x86)\MySQL\MySQL Server 6.0\bin"; public String uname = "root"; public String upass = "root"; public String dbname = "losscar_db"; public int hour=18; public Form1() { InitializeComponent(); timer1.Interval=1000*10; timer1.Start(); txt_uname.Text = uname; txt_upass.Text = upass; txt_dbname.Text = dbname; txt_bakpath.Text = bakpath; txt_mysql.Text = appDirecroty; txt_hour.Text = hour.ToString(); } /// <summary> /// 执行Cmd命令 /// </summary> /// <param name="workingDirectory">要启动的进程的目录 /// <param name="command">要执行的命令 public static void StartCmd(String workingDirectory, String command) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.WorkingDirectory = workingDirectory; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(command); p.StandardInput.WriteLine("exit"); } private void btn_bak_Click(object sender, EventArgs e) { try { //String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址"; //构建执行的命令 StringBuilder sbcommand = new StringBuilder(); StringBuilder sbfileName = new StringBuilder(); sbfileName.AppendFormat("{0}", DateTime.Now.ToShortDateString()).Replace("-", "").Replace(":", "").Replace(" ", "").Replace("/", ""); String fileName = sbfileName.ToString(); String directory = bakpath + fileName + ".bak"; sbcommand.AppendFormat("mysqldump --quick --host=localhost --default-character-set=utf8 --lock-tables --verbose --force --port=3306 --user={0} --password={1} {2} -r \"{3}\"", uname, upass, dbname, directory); String command = sbcommand.ToString(); //获取mysqldump.exe所在路径 //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\"; StartCmd(appDirecroty, command); MessageBox.Show(@"数据库已成功备份到 " + directory + " 文件中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("数据库备份失败!"); } } public void bakup_db() { try { //String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址"; //构建执行的命令 StringBuilder sbcommand = new StringBuilder(); StringBuilder sbfileName = new StringBuilder(); sbfileName.AppendFormat("{0}", DateTime.Now.ToShortDateString()).Replace("-", "").Replace(":", "").Replace(" ", "").Replace("/", ""); String fileName = sbfileName.ToString(); String directory = bakpath + fileName+".bak"; sbcommand.AppendFormat("mysqldump --quick --host=localhost --default-character-set=utf8 --lock-tables --verbose --force --port=3306 --user={0} --password={1} {2} -r \"{3}\"", uname, upass, dbname, directory); String command = sbcommand.ToString(); //获取mysqldump.exe所在路径 //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\"; StartCmd(appDirecroty, command); } catch (Exception ex) { } } private void btn_recovery_Click(object sender, EventArgs e) { recovery_db(); } public void recovery_db() { //string s = "mysql --port=端口号 --user=用户名 --password=密码 数据库名

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

MySQL和phpMyAdmin是強大的數據庫管理工具。 1)MySQL用於創建數據庫和表、執行DML和SQL查詢。 2)phpMyAdmin提供直觀界面進行數據庫管理、表結構管理、數據操作和用戶權限管理。

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。MySQL以其高性能、可扩展性和跨平台支持著称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

在開發一個小型應用時,我遇到了一個棘手的問題:需要快速集成一個輕量級的數據庫操作庫。嘗試了多個庫後,我發現它們要么功能過多,要么兼容性不佳。最終,我找到了minii/db,這是一個基於Yii2的簡化版本,完美地解決了我的問題。

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

在使用Thelia開發電商網站時,我遇到了一個棘手的問題:MySQL模式設置不當,導致某些功能無法正常運行。經過一番探索,我找到了一個名為TheliaMySQLModesChecker的模塊,它能夠自動修復Thelia所需的MySQL模式,徹底解決了我的困擾。

在MySQL中,外鍵的作用是建立表與表之間的關係,確保數據的一致性和完整性。外鍵通過引用完整性檢查和級聯操作維護數據的有效性,使用時需注意性能優化和避免常見錯誤。

MySQL和MariaDB的主要區別在於性能、功能和許可證:1.MySQL由Oracle開發,MariaDB是其分支。 2.MariaDB在高負載環境中性能可能更好。 3.MariaDB提供了更多的存儲引擎和功能。 4.MySQL採用雙重許可證,MariaDB完全開源。選擇時應考慮現有基礎設施、性能需求、功能需求和許可證成本。
