怎样用WinForm实现MySql的异机备份还原功能 如题,两个按钮,一个是备份,一个是还原,网上看了好多都不靠谱额。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最简单的方法,就是直接调用 操作系统命令 mysqldmp 来备份, 和调用 mysql.exe 来恢复。 我知道是用mysqldmp来实现,我写了个Demo,备份和导出的都没用啊,导不出://辅助类using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace eFormPC.DBUtility{ public class MySQLCmd { /// <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"); } }}//备份private void button8_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 --character-sets-dir=utf-8 --user=root --password=1 tabletpc -r \"{0}\"", @"C:\123.sql"); String command = sbcommand.ToString(); //获取mysqldump.exe所在路径 //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\"; String appDirecroty = @"C:\Program Files\MySQL\MySQL Server 5.0\bin\"; MySQLCmd.StartCmd(appDirecroty, command); MessageBox.Show(@"数据库已成功备份到 " + directory + " 文件中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show("数据库备份失败!"); } }://还原private void button9_Click(object sender, EventArgs e) { try { StringBuilder sbcommand = new StringBuilder(); OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { String directory = openFileDialog.FileName; //在文件路径后面加上""避免空格出现异常 sbcommand.AppendFormat("mysql --user=root --password=1 tabletpc <c:\\123.sql"); String command = sbcommand.ToString(); //获取mysql.exe所在路径 String appDirecroty = @"C:\Program Files\MySQL\MySQL Server 5.0\bin\"; DialogResult result = MessageBox.Show("您是否真的想覆盖以前的数据库吗?那么以前的数据库数据将丢失!!!", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { MySQLCmd.StartCmd(appDirecroty, command); MessageBox.Show("数据库还原成功!"); } } } catch (Exception ex) { MessageBox.Show("数据库还原失败!"); } } 是导出到123.sql,但是还原回来的话还原不了,还是当前状态 123.sql中的内容是什么,你怎么判断的 “还是当前状态”? 也就是说你的导出已经成功了,只是无法导入?直接到WIDNOS的DOS下试一下这个命令。mysql --user=root --password=1 tabletpc <c:\\123.sql 还有个问题就是我连127.0.0.1时能导入成功,但是连接远程比如192.168.0.123也能导出但导出的却是0字节,"mysqldump --character-sets-dir=utf-8 --user=root --password=1 tabletpc -r \"{0}\"", @"C:\123.sql");这个语句怎么连接远程啊,好像默认就没有host名称 没有连接权限!参考下贴中的检查方法并贴出你的检查结果(#43楼)http://topic.csdn.net/u/20090920/22/14d4f597-b7d4-4c24-b0db-abb2956e66c3.html[收集]mysql 无法联接故障现象及原因 用程序中的相同用户登录MYSQL,在命令行下用MYSQL -U -P<c:\\123.sql能否导入 ACMAIN_CHM,刚才那问题续 mysql如何在新建表时以日期命名表名 mysql可以实现,同一个数据库中,不同的表应用不存的存贮引擎吗? 求教EMS SQL Manager Lite for Mysql mysql 5.0.15 采用innodb表类型,无法插入中文? 怎样写MySQL的脚本 求救:如何把MSSQL的数据库转换成MYSQL,谢谢 Help! PostgreSQL在我的windows系统中无法安装! 关于PostgreSQL的一个问题 mysql查询数据计算 sql问题急用!求大神 mysql 存储过程 怪现象 急!!!!
//辅助类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;namespace eFormPC.DBUtility
{
public class MySQLCmd
{
/// <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");
} }
}//备份
private void button8_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 --character-sets-dir=utf-8 --user=root --password=1 tabletpc -r \"{0}\"", @"C:\123.sql");
String command = sbcommand.ToString(); //获取mysqldump.exe所在路径
//String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
String appDirecroty = @"C:\Program Files\MySQL\MySQL Server 5.0\bin\";
MySQLCmd.StartCmd(appDirecroty, command);
MessageBox.Show(@"数据库已成功备份到 " + directory + " 文件中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
catch (Exception ex)
{
MessageBox.Show("数据库备份失败!");
}
}://还原
private void button9_Click(object sender, EventArgs e)
{
try
{
StringBuilder sbcommand = new StringBuilder(); OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK)
{
String directory = openFileDialog.FileName; //在文件路径后面加上""避免空格出现异常
sbcommand.AppendFormat("mysql --user=root --password=1 tabletpc <c:\\123.sql");
String command = sbcommand.ToString(); //获取mysql.exe所在路径
String appDirecroty = @"C:\Program Files\MySQL\MySQL Server 5.0\bin\"; DialogResult result = MessageBox.Show("您是否真的想覆盖以前的数据库吗?那么以前的数据库数据将丢失!!!", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
MySQLCmd.StartCmd(appDirecroty, command);
MessageBox.Show("数据库还原成功!");
}
} }
catch (Exception ex)
{
MessageBox.Show("数据库还原失败!");
} }
mysql --user=root --password=1 tabletpc <c:\\123.sql
"mysqldump --character-sets-dir=utf-8 --user=root --password=1 tabletpc -r \"{0}\"", @"C:\123.sql");
这个语句怎么连接远程啊,好像默认就没有host名称
[收集]mysql 无法联接故障现象及原因
在命令行下用
MYSQL -U -P<c:\\123.sql
能否导入