///執(zhí)行Cmd命" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

C#備份還原MySql數(shù)據(jù)庫

系統(tǒng) 2990 0

??????調(diào)用MySql的工具mysqldump來實(shí)現(xiàn)。

??????類Cmd來實(shí)現(xiàn)調(diào)用cmd命令,
要啟動的進(jìn)程所在的目錄是說mysql自動的備份還原數(shù)據(jù)庫工具mysqldump和mysql所在目錄,當(dāng)然,這個方法可以執(zhí)行別的命令行工具。

?

代碼
using ?System;
using ?System.Collections.Generic;
using ?System.Text;
using ?System.Diagnostics;

????
public ? class ?Cmd
????{
????????
/// ? <summary>
????????
/// ?執(zhí)行Cmd命令
????????
/// ? </summary>
????????
/// ? <param?name="workingDirectory"> 要啟動的進(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=密碼?數(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(
@" 數(shù)據(jù)庫已成功備份到? " ? + ?directory? + ? " ?文件中 " ,? " 提示 " ,?MessageBoxButtons.OK,?MessageBoxIcon.Information);
????????????????}

????????????}
????????????
catch ?(Exception?ex)
????????????{
????????????????MessageBox.Show(
" 數(shù)據(jù)庫備份失敗! " );
????????????????
????????????}
????????}
????????
????}
}

?

?

還原方法,調(diào)用的是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=密碼?數(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=密碼?數(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ù)庫還原失敗! " );
????????????}

????????}
????????
????}
}

?

?

C#備份還原MySql數(shù)據(jù)庫


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产这里只有精品 | www色综合| 国产精品久久久AV久久久 | 日韩av电影免费看 | 国产毛片a级 | 欧美精品99毛片免费高清观看 | 欧美激情精品久久久久 | 久久99热只有视精品6国产 | 亚洲综合日韩欧美一区二区三 | 国产精品久久久久一区二区 | 清纯唯美亚洲综合激情 | 久久精品欧美一区二区三区不卡 | 91偷拍精品一区二区三区 | 欧美aaa级片 | 久久天天躁夜夜躁狠狠 | 麻豆网站入口 | 在线欧美一区 | 欧美成人精品二区三区99精品 | 精品视频久久久久 | 狠狠av| 久久久美女 | 宝贝jojo第三季 | 国产一级做a爰片久久毛片 欧美一区欧美二区 | 波多野结衣三级视频 | 日本亚洲一区二区 | 看免费的毛片 | 黄色av.com| 日韩一区二区av | 999精品免费视频观看 | 亚洲综合色视频在线观看 | 欧美一区二区三区成人 | 欧美视频精品一区二区三区 | 波多野结衣在线观看一区 | 中文字幕日韩欧美一区二区三区 | 一级毛片免费在线播放 | 精品成人 | 日本一区二区视频在线 | 欧美视频在线一区 | 欧美淫| 精品三级国产精品经典三 | 成人在线不卡 |