C#备份还原MySql数据库
生活随笔
收集整理的這篇文章主要介紹了
C#备份还原MySql数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文:C#備份還原MySql數據庫
using?System.Collections.Generic;
using?System.Text;
using?System.Diagnostics;
????public?class?Cmd
????{
????????///?<summary>
????????///?執行Cmd命令
????????///?</summary>
????????///?<param?name="workingDirectory">要啟動的進程的目錄</param>
????????///?<param?name="command">要執行的命令</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.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=密碼?數據庫名?-r?備份到的地址";
????????????????//構建執行的命令
????????????????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=密碼?數據庫名?-r?\"{0}\"",?directory);
????????????????????String?command?=?sbcommand.ToString();
????????????????????//獲取mysqldump.exe所在路徑
????????????????????String?appDirecroty?=?System.Windows.Forms.Application.StartupPath?+?"\\";
????????????????????Cmd.StartCmd(appDirecroty,?command);
????????????????????MessageBox.Show(@"數據庫已成功備份到?"?+?directory?+?"?文件中",?"提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show("數據庫備份失敗!");
????????????????
????????????}
????????}
????????
????}
}
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=密碼?數據庫名<還原文件所在路徑";
????????????try
????????????{
????????????????StringBuilder?sbcommand?=?new?StringBuilder();
????????????????OpenFileDialog?openFileDialog?=?new?OpenFileDialog();
????????????????if?(openFileDialog.ShowDialog()?==?DialogResult.OK)
????????????????{
????????????????????String?directory?=?openFileDialog.FileName;
????????????????????//在文件路徑后面加上""避免空格出現異常
????????????????????sbcommand.AppendFormat("mysql?--host=localhost?--default-character-set=gbk?--port=端口號?--user=用戶名?--password=密碼?數據庫<\"{0}\"",?directory);
????????????????????String?command?=?sbcommand.ToString();
????????????????????//獲取mysql.exe所在路徑
????????????????????String?appDirecroty?=?System.Windows.Forms.Application.StartupPath?+?"\\";
????????????????????DialogResult?result?=?MessageBox.Show("您是否真的想覆蓋以前的數據庫嗎?那么以前的數據庫數據將丟失!!!",?"警告",?MessageBoxButtons.YesNo,?MessageBoxIcon.Warning);
????????????????????if?(result?==?DialogResult.Yes)
????????????????????{
????????????????????????Cmd.StartCmd(appDirecroty,?command);
????????????????????????MessageBox.Show("數據庫還原成功!");
????????????????????}
????????????????}
????????????????
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show("數據庫還原失敗!");
????????????}
????????}
????????
????}
}
??????項目結束,粘點代碼出來讓Google或Baidu一下,原因是現在還搜不到這么現成的
??????調用MySql的工具mysqldump來實現。
??????類Cmd來實現調用cmd命令,
要啟動的進程所在的目錄是說mysql自動的備份還原數據庫工具mysqldump和mysql所在目錄,當然,這個方法可以執行別的命令行工具。
?
using?System;using?System.Collections.Generic;
using?System.Text;
using?System.Diagnostics;
????public?class?Cmd
????{
????????///?<summary>
????????///?執行Cmd命令
????????///?</summary>
????????///?<param?name="workingDirectory">要啟動的進程的目錄</param>
????????///?<param?name="command">要執行的命令</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=密碼?數據庫名?-r?備份到的地址";
????????????????//構建執行的命令
????????????????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=密碼?數據庫名?-r?\"{0}\"",?directory);
????????????????????String?command?=?sbcommand.ToString();
????????????????????//獲取mysqldump.exe所在路徑
????????????????????String?appDirecroty?=?System.Windows.Forms.Application.StartupPath?+?"\\";
????????????????????Cmd.StartCmd(appDirecroty,?command);
????????????????????MessageBox.Show(@"數據庫已成功備份到?"?+?directory?+?"?文件中",?"提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show("數據庫備份失敗!");
????????????????
????????????}
????????}
????????
????}
}
?
還原方法,調用的是mysql自帶工具mysql,還原時要注意的是選擇的文件所在路徑時,文件名要是有空格的話會出
異常,所以在文件路徑名加上雙引號""
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=密碼?數據庫名<還原文件所在路徑";
????????????try
????????????{
????????????????StringBuilder?sbcommand?=?new?StringBuilder();
????????????????OpenFileDialog?openFileDialog?=?new?OpenFileDialog();
????????????????if?(openFileDialog.ShowDialog()?==?DialogResult.OK)
????????????????{
????????????????????String?directory?=?openFileDialog.FileName;
????????????????????//在文件路徑后面加上""避免空格出現異常
????????????????????sbcommand.AppendFormat("mysql?--host=localhost?--default-character-set=gbk?--port=端口號?--user=用戶名?--password=密碼?數據庫<\"{0}\"",?directory);
????????????????????String?command?=?sbcommand.ToString();
????????????????????//獲取mysql.exe所在路徑
????????????????????String?appDirecroty?=?System.Windows.Forms.Application.StartupPath?+?"\\";
????????????????????DialogResult?result?=?MessageBox.Show("您是否真的想覆蓋以前的數據庫嗎?那么以前的數據庫數據將丟失!!!",?"警告",?MessageBoxButtons.YesNo,?MessageBoxIcon.Warning);
????????????????????if?(result?==?DialogResult.Yes)
????????????????????{
????????????????????????Cmd.StartCmd(appDirecroty,?command);
????????????????????????MessageBox.Show("數據庫還原成功!");
????????????????????}
????????????????}
????????????????
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show("數據庫還原失敗!");
????????????}
????????}
????????
????}
}
?
?
?
?
總結
以上是生活随笔為你收集整理的C#备份还原MySql数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线性方程组的非零解
- 下一篇: [Apache]网站页面静态化与Apac