C# MySql 数据数据备份与还原

来源:互联网 发布:千牛mac历史版本 编辑:程序博客网 时间:2024/06/07 19:58

需要先下载MySql.exe  和MySqlDump.exe

备份:

/// <summary>        /// 数据库备份        /// </summary>        /// <param name="ary">0-数据库地址  1-端口  2-用户名  3-密码  4-数据库名称</param>        private void DataBeiFen(string[] ary)        {            try            {                #region 数据备份                //System.Windows.Forms.SaveFileDialog saveFile = new System.Windows.Forms.SaveFileDialog();                //saveFile.InitialDirectory = "BackData" + "\\";//默认路径                //saveFile.FilterIndex = 1;        //默认值为第一个                //saveFile.RestoreDirectory = true;      //重新定位保存路径                //saveFile.Filter = "备份文件 (*.bak)|*.bak|所有文件 (*.*)|*.*";//筛选器,定义文件类型                //if (saveFile.ShowDialog() == DialogResult.OK)                //{                //    if (!File.Exists(saveFile.FileName.ToString())) //判断是否有重名                //    {                //        string sqlStr = "BACKUP DATABASE power_ii TO DISK = '" + saveFile.FileName.ToString() + "'";                //        if (BackUpBll.ExecuteSql(sqlStr) > 0)                //        {                //            MessageBox.Show("数据备份成功!", "提示对话框");                //        }                //    }                //    else                //    {                //        MessageBox.Show("请重新命名!", "提示对话框");                //    }                //}                 #endregion                System.Windows.Forms.SaveFileDialog saveFile = new System.Windows.Forms.SaveFileDialog();                saveFile.InitialDirectory = "BackData" + "\\";//默认路径                saveFile.FilterIndex = 1;        //默认值为第一个                saveFile.RestoreDirectory = true;      //重新定位保存路径                saveFile.Filter = "备份文件 (*.bak)|*.bak|所有文件 (*.*)|*.*";//筛选器,定义文件类型                if (saveFile.ShowDialog() == DialogResult.OK)                {                    if (!File.Exists(saveFile.FileName.ToString())) //判断是否有重名                    {                        string host = ary[0];                        string port = ary[1];                        string user = ary[2];                        string password = ary[3];                        string database = ary[4];                        string fileName = database + "_bak_" + DateTime.Now.ToString("yyyyMMddhhmmss");                        string cmdStr = "mysqldump -h" + host + " -P" + port + " -u" + user + " -p" + password + " " + database + " > " + saveFile.FileName.ToString();                        //System.Diagnostics.Process.Start("cmd", cmdStr);                        System.Diagnostics.Process p = new System.Diagnostics.Process();                        p.StartInfo.FileName = "cmd.exe";//要执行的程序名称                        p.StartInfo.UseShellExecute = false;                        p.StartInfo.WorkingDirectory = System.Windows.Forms.Application.StartupPath + "\\";                        p.StartInfo.RedirectStandardInput = true;//可能接受来自调用程序的输入信息                        p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息                        p.StartInfo.CreateNoWindow = true;//不显示程序窗口                        p.Start();//启动程序                        p.StandardInput.WriteLine(cmdStr);                        p.StandardInput.WriteLine("exit");                        MessageBox.Show("数据库已成功备份", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                    }                    else                    {                        MessageBox.Show("无法替换文件,请重新命名!", "提示对话框");                    }                }            }            catch (Exception ex)            {                errorlog.WriLog(ErrorLog.LogType.Error, "异常", ex);                return;            }        }



还原:

/// <summary>        /// 数据库恢复        /// </summary>        /// <param name="ary">0-数据库地址  1-端口  2-用户名  3-密码  4-数据库名称</param>        private void DataRestore(string[] ary)        {            System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();            openFile.InitialDirectory = "BackData" + "\\";//默认路径            openFile.FilterIndex = 1;            openFile.Filter = "备份文件 (*.bak)|*.bak|所有文件 (*.*)|*.*";//筛选器,定义文件类型            if (openFile.ShowDialog() == DialogResult.OK)            {                string path = openFile.FileName;//获得备份路径                BackUpBll.ExecuteSql("CREATE DATABASE " + ary[4] + "");                try                {                    string host = ary[0];                    string port = ary[1];                    string user = ary[2];                    string password = ary[3];                    string database = ary[4];                    string fileName = database + "_bak_" + DateTime.Now.ToString("yyyyMMddhhmmss");                    string cmdStr = "mysql -h"+host+" -P"+port+" -u"+user+" -p"+password+" "+database+"  < " + path + "";                    System.Diagnostics.Process p = new System.Diagnostics.Process();                    p.StartInfo.FileName = "cmd.exe";//要执行的程序名称                    p.StartInfo.UseShellExecute = false;                    p.StartInfo.WorkingDirectory = System.Windows.Forms.Application.StartupPath + "\\";                    p.StartInfo.RedirectStandardInput = true;//可能接受来自调用程序的输入信息                    p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息                    p.StartInfo.CreateNoWindow = true;//不显示程序窗口                    p.Start();//启动程序                    p.StandardInput.WriteLine(cmdStr);                    p.StandardInput.WriteLine("exit");                   DialogResult result= MessageBox.Show("数据加载成功,单击OK将重启软件!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);                   if (result == DialogResult.OK)                   {                       if (_main != null)                       {                           _main.RestartState = 1;                           Application.Restart();                       }                   }                }                catch (Exception ex)                {                    errorlog.WriLog(ErrorLog.LogType.Error, "异常", ex);                    return;                }            }        }


0 0
原创粉丝点击