C#备份还原MySql数据库
调⽤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 = "";
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();
//获取所在路径
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;
mysql下载appusing 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();
//获取所在路径
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("数据库还原失败!");
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论