这几天一直在做学校的C#期末项目,其中有一个功能就是数据的备份与恢复,在百度了一番之后,只找到了适用于SQL Server的备份SQL语句
BACKUP DATABASE database_name
TO DISK = 'physical_device_name'
[ WITH { DIFFERENTIAL
| COPY_ONLY
| { COMPRESSION | NO_COMPRESSION }
| { NOINIT | INIT }
| { NOSKIP | SKIP }
| { NOFORMAT | FORMAT }
| STATS [ = percentage ] }]
但是,MySQL使用的是mysqldump命令来进行数据库的备份操作,没有SQL语句,怎么办呢,于是,“曲线救国”,既然mysqldump命令是在cmd中运行的,那为什么不把命令写在.bat文件里再运行.bat文件呢?说做就做,这个功能的界面是这样的
这是选择备份目标目录带的选择按钮的事件代码:
private void button3_Click(object sender, EventArgs e){FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();//设置根目录在桌面;myFolderBrowserDialog.RootFolder = System.Environment.SpecialFolder.Desktop;//设置当前选择的路径myFolderBrowserDialog.SelectedPath = "C:";//允许在对话框中包括一个新建目录的按钮myFolderBrowserDialog.ShowNewFolderButton = true;//设置对话框的说明信息myFolderBrowserDialog.Description = "请选择输出目录";if (myFolderBrowserDialog.ShowDialog() == DialogResult.OK){//确认是否保存string strPath = myFolderBrowserDialog.SelectedPath;//获取路径textBox1.Text = strPath;//赋值给文本显示}}
这是数据备份按钮的事件代码(-u-->UserName -p-->PassWord)
private void button2_Click(object sender, EventArgs e){if (MessageBox.Show("是否备份数据", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)//弹出对话框,等待用户选择{if (textBox1.Text.ToString() != "")//空值检测{string strRiQi = DateTime.Now.Year.ToString() + (DateTime.Now.Month.ToString().Length < 2 ? 0 + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + (DateTime.Now.Day.ToString().Length < 2 ? 0 + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + (DateTime.Now.Hour.ToString().Length < 2 ? 0 + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString()) + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();//生成日期时间try{Process proc = null;//创建Processtry{string backup =String.Format("mysqldump -u{3} -p{1} {2} >{0}"+"\\"+ "DataBackUp"+strRiQi+".dbak",textBox1.Text,getLine2(7),getLine2(5),getLine2(6));//初始化语句File.WriteAllText(@"C:\Program Files\MySQL\MySQL Server 5.7\bin\my.bat", backup);//写入文件string targetDir = string.Format(@"C:\Program Files\MySQL\MySQL Server 5.7\bin\");proc = new Process();proc.StartInfo.WorkingDirectory = targetDir;proc.StartInfo.FileName = "my.bat";proc.StartInfo.Arguments = string.Format("10");proc.StartInfo.CreateNoWindow = false;proc.Start();proc.WaitForExit();}catch (Exception ex){Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());}MessageBox.Show("备份成功!");}catch (Exception e1){MessageBox.Show("备份失败");Console.WriteLine(e1);}}else{MessageBox.Show("请选择保存路径!");}}}
这里说明一下,我的数据库配置写在了本地文件上,代码中的getLine()方法是用来提取文件中相应行数的字符串,下面是getLine()方法的代码:
public static string getLine2(int ii){string line = null;string file = "DataConfig.txt";if (File.Exists(@"c:\xinyutian\DataConfig.txt")){//存在 string[] lines = File.ReadAllLines("c:\\xinyutian\\" + file);for (int i = 0; i < lines.Length; i++){if (i == ii){line = lines[i];}}}else{//不存在 }return line;}
这里遇到一个问题,就是我已经设置了环境变量,在cmd窗口里已经可以运行mysqldump命令了,但是在程序中却提示“mysqldump不是内部或外部命令,也不是可运行的程序或批处理文件。”无奈,只能参考《C#程序以管理员权限运行》,添加app.manifest,将程序以管理员身份运行,将.bat文件新建在MYSQL的目录下了......
<!-- UAC 清单选项如果想要更改 Windows 用户帐户控制级别,请使用以下节点之一替换 requestedExecutionLevel 节点。n<requestedExecutionLevel level="asInvoker" uiAccess="false" /><requestedExecutionLevel level="requireAdministrator" uiAccess="false" /><requestedExecutionLevel level="highestAvailable" uiAccess="false" />指定 requestedExecutionLevel 元素将禁用文件和注册表虚拟化。如果你的应用程序需要此虚拟化来实现向后兼容性,则删除此元素。-->
到这里,如果没有差错的话,应该就会在点击按钮后弹出熟悉的cmd窗口,命令一闪而过,相应的数据库备份文件已经在你指定的路径下了