C# MySQL数据库的备份 还原 初始化

主要功能:

1,数据库的初始化

2,数据库的备份|还原

代码如下:

// 执行创建数据库操作
this.GetExecute(G_Con, "create database if not exists NEWDB");


this.sqlAddress = " -h " + IP + " -u" + User + " -p" + Password + " NEWDB ";
// 数据库的备份
private void btn_Dump_Click(object sender, EventArgs e)
{
    using (SaveFileDialog sfd = new SaveFileDialog())
    {
        sfd.Filter = "数据库文件|*.sql";
        sfd.FilterIndex = 0;
        sfd.RestoreDirectory = true;
        sfd.FileName = "BackUp" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".sql";

        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string filePath = sfd.FileName;
            string cmd = "mysqldump " + sqlAddress + " > \"" + filePath + "\"";
            string result = RunCmd(cmd);
            if (result.Trim() == "")
            {
                MessageBox.Show("数据库备份成功!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(result, "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
          
        }
    }
}
//数据库的还原
// 还原数据库
private void btn_Import_Click(object sender, EventArgs e)
{
    if (this.tb_Path.Text.Trim() == "")
    {
        MessageBox.Show("请选择要恢复的文件!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
        return; 
    }
    //this.GetExecute(G_Con, "create database if not exists clothes");
    string filePath = this.tb_Path.Text.Trim();
    string cmd = "mysql " + sqlAddress + " < \"" + filePath + "\"";
    string result = RunCmd(cmd);
    if (result.Trim() == "")
    {
        MessageBox.Show("数据库恢复成功!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        MessageBox.Show(result, "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}
// 命令行操作
private string RunCmd(string command)
{
    //例Process
    Process p = new Process();

    p.StartInfo.FileName = "cmd.exe";           //确定程序名
    p.StartInfo.Arguments = "/c " + command;    //确定程式命令行
    p.StartInfo.UseShellExecute = false;        //Shell的使用
    p.StartInfo.RedirectStandardInput = true;   //重定向输入
    p.StartInfo.RedirectStandardOutput = true; //重定向输出
    p.StartInfo.RedirectStandardError = true;   //重定向输出错误
    p.StartInfo.CreateNoWindow = true;          //设置置不显示示窗口
    p.Start();   //00

    p.StandardInput.WriteLine(command);       //也可以用这种方式输入入要行的命令

    p.StandardInput.WriteLine("exit");        //要得加上Exit要不然下一行程式
    //p.WaitForExit();
    //p.Close();
    //return p.StandardOutput.ReadToEnd();        //输出出流取得命令行结果果
    return p.StandardError.ReadToEnd();
}

 

主要原理就是 使用MYSQL的命令 mysqldump 和 mysql 进行导出 和导入操作,其中调用了windows的 命令行。

一开始的时候使用了Navicat for MySQL 进行了数据库的备份,然后还原的时候发现一台机器正常,一台机器出错,郁闷,后来发现应该是

这个工具的mysql命令跟安装mysql命令版本不同的原因,使用这个工具导出的数据文件内容格式,跟程序中导出的数据文件内容格式不同。

当都采用程序导出的文件作为 还原文件时,2台机器都正常。不过还是推荐下Navicat for MySQL,用着还不错。多类型数据库 datastudio 是王道!

 

注意:如果出现命令不存在错误,请在系统环境变量中的 PATH 中 增加 MYSQL 的安装路径,如:C:\Program Files\MySQL\MySQL Server 5.5\bin

 

 

 

posted on 2012-04-17 10:14  Rush_Sykes  阅读(1820)  评论(0编辑  收藏  举报