首页 > 代码库 > C#备份还原MySql数据库

C#备份还原MySql数据库

原文:C#备份还原MySql数据库

      项目结束,粘点代码出来让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;
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(
"数据库还原失败!");
            }

        }
        
    }
}