C#備份還原MySQL數(shù)據(jù)庫的應(yīng)用
以下的文章主要介紹的是C#備份還原MySQL數(shù)據(jù)庫的實(shí)際應(yīng)用,如果你對C#備份還原MySQL數(shù)據(jù)庫的實(shí)際應(yīng)用很感興趣的話,你就可以點(diǎn)擊以下的文章了,望你會(huì)對其相關(guān)的內(nèi)容有跟你更深入的了解。
通過調(diào)用MySQL的工具M(jìn)ySQLdump來實(shí)現(xiàn)。
類Cmd來實(shí)現(xiàn)調(diào)用cmd命令,
要啟動(dòng)的進(jìn)程所在的目錄是說MySQL自動(dòng)的備份還原MySQL數(shù)據(jù)庫工具M(jìn)ySQLdump和MySQL所在目錄,當(dāng)然,這個(gè)方法可以執(zhí)行別的命令行工具。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- public class Cmd
- {
- / <summary>
/ 執(zhí)行Cmd命令
/ </summary>
/ <param name="workingDirectory">要啟動(dòng)的進(jìn)程的目錄</param>
/ <param name="command">要執(zhí)行的命令</param>
- 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");
- }
- }
備份方法:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- using System.Configuration;
- using MDRClient.DataAccess;
- namespace MDRClient
- {
- public partial class DataBackup : Form
- {
- public DataBackup()
- {
- InitializeComponent();
- }
- private void btnBackup_Click(object sender, EventArgs e)
- {
- try
- {
String command = "MySQLdump --quick --host=localhost --default- character-set=gb2312 --lock-tables --verbose --force --port=端口號 --user= 用戶名 --password=密碼 MySQL數(shù)據(jù)庫名 -r 備份到的地址";
構(gòu)建執(zhí)行的命令
- StringBuilder sbcommand = new StringBuilder();
- StringBuilder sbfileName = new StringBuilder();
- sbfileName.AppendFormat("{0}", DateTime.Now.ToString()).Replace("-", "").Replace(":", "").Replace(" ", "");
- String fileName = sbfileName.ToString();
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.AddExtension = false;
- saveFileDialog.CheckFileExists = false;
- saveFileDialog.CheckPathExists = false;
- saveFileDialog.FileName = fileName;
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- String directory = saveFileDialog.FileName;
- sbcommand.AppendFormat("MySQLdump --quick --host=localhost --default- character-set=gbk
--lock-tables --verbose --force --port=端口號 --user=用戶 名 --password=密碼 數(shù)據(jù)庫名 -r \"{0}\"", directory);- String command = sbcommand.ToString();
獲取MySQLdump.exe所在路徑
- String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
- Cmd.StartCmd(appDirecroty, command);
MessageBox.Show(@"MySQL數(shù)據(jù)庫已成功備份到 " + directory + " 文件中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("數(shù)據(jù)庫備份失??!");
- }
- }
- }
- }
還原方法,調(diào)用的是MySQL自帶工具M(jìn)ySQL,還原時(shí)要注意的是選擇的文件所在路徑時(shí),文件名要是有空格的話會(huì)出異常,所以在文件路徑名加上雙引號""
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- using System.Configuration;
- using MDRClient.DataAccess;
- namespace MDRClient
- {
- public partial class DataRestore : Form
- {
- public DataRestore()
- {
- InitializeComponent();
- }
- private void btnRestore_Click(object sender, EventArgs e)
- {
string s = "MySQL --port=端口號 --user=用戶 名 --password=密碼 數(shù)據(jù)庫名<還原文件所在路徑";
- try
- {
- StringBuilder sbcommand = new StringBuilder();
- OpenFileDialog openFileDialog = new OpenFileDialog();
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- String directory = openFileDialog.FileName;
在文件路徑后面加上""避免空格出現(xiàn)異常
sbcommand.AppendFormat("MySQL --host=localhost --default-character-set=gbk --port=端口 號 --user=用戶名 --password=密碼 MySQL數(shù)據(jù)庫<\"{0}\"", directory);
- String command = sbcommand.ToString();
獲取MySQL.exe所在路徑
- String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
DialogResult result = MessageBox.Show("您是否真的想覆蓋以前的數(shù)據(jù)庫嗎?那么以前的數(shù)據(jù)庫數(shù)據(jù)將丟失?。?!", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
- if (result == DialogResult.Yes)
- {
- Cmd.StartCmd(appDirecroty, command);
- MessageBox.Show("數(shù)據(jù)庫還原成功!");
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("數(shù)據(jù)庫還原失敗!");
- }
- }
- }
- }
以上的相關(guān)內(nèi)容就是對MySQL數(shù)據(jù)庫的介紹,望你能有所收獲。
【編輯推薦】