C#备份还原MySql数据库

系统 2128 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条评论