///執行Cmd命" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

C#備份還原MySql數據庫

系統 2906 0

??????調用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;
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數據庫


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 五月天婷婷在线视频国产在线 | 精品伊人久久大香线蕉网站 | 国产高清美女一级a毛片久久w | 久久精品视频免费播放 | 亚洲精品911| 国产精品视频一区麻豆 | 日日噜噜夜夜狠狠tv视频免费 | 四虎影永久在线观看精品 | 福利院肉动漫视频在线观看 | 国产精品亚洲欧美日韩一区在线 | 九九久久免费视频 | 亚洲狠狠网站色噜噜 | 伊人55影院| 国内精品视频在线观看 | 在线观看日本中文字幕 | 亚洲香蕉毛片久久网站老妇人 | 精品视自拍视频在线观看 | 超级碰碰青草久热国产 | 国产视频综合 | 一级毛片成人免费看免费不卡 | 亚洲国产精品线观看不卡 | 国产亚洲第一精品社区麻豆 | 四虎精品| 99热久久国产精品免费看 | 国产精品福利一区二区 | 综合精品视频 | 欧美黄色三级视频 | 欧美一级毛片免费高清的 | 久久精品99久久香蕉国产色戒 | 久草新免费 | 四虎影永久地址www 四虎影永久在线高清免费 四虎影永久在线观看精品 四虎影永久在线观看网址 四虎影院.com | 中文字幕综合久久久久 | 久草香蕉在线视频 | 国产精品久久久久不卡绿巨人 | 6080一级毛片 | 国产精品午夜波多野结衣性色 | 男人叼女人的痛爽视频免费 | 久草手机在线观看 | 九九色影院 | 日韩日b| 中文字幕在线播放 |