c#+mysql+表备份_C# MySql 数据数据备份与还原

该博客介绍了如何使用C#结合MySqlDump.exe工具进行MySQL数据库的备份和还原操作。通过创建SaveFileDialog和OpenFileDialog,用户可以选择备份或恢复的文件,然后通过执行命令行命令进行实际操作。在备份过程中,程序会检查是否存在重名文件,确保备份的唯一性;在恢复时,首先创建新的数据库,然后将备份文件导入。
摘要由CSDN通过智能技术生成

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

备份:

/// /// 数据库备份

///

/// 0-数据库地址 1-端口 2-用户名 3-密码 4-数据库名称

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;

}

}

还原:

/// /// 数据库恢复

///

/// 0-数据库地址 1-端口 2-用户名 3-密码 4-数据库名称

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值