ASP.NET备份还原MySql数据库 Dump
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 UILogic.ClearCache(); 4 VPUserInfo userinfo = UILogic.getSession() as VPUserInfo; 5 if (userinfo == null) 6 { 7 //Response.Write("<mce:script type="text/javascript"><!-- 8 top.location.href='~/../login.aspx' 9 // --></mce:script>"); 10 } 11 else 12 { 13 //if (!Page.IsPostBack) 14 //{ 15 System.IO.Directory.CreateDirectory("C://Program Files//MySQL//MySQL Server 5.0//data//vpdata");//在服务器mysql中创建vpdata数据库 16 //} 17 } 18 } 19 20 protected void Backup_Click(object sender, ImageClickEventArgs e) 21 { 22 try 23 { 24 //string filename = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", ""); 25 26 string filePath = "D://web//App_Data"; 27 string fileName = filePath + "//vpdata.sql";//备份的文件名称和路径 28 //判断目录是否存在 29 if (!System.IO.File.Exists(fileName)) 30 { 31 System.IO.Directory.CreateDirectory(filePath); 32 } 33 34 //构建执行的命令 35 String command = string.Format("mysqldump --quick --host=localhost --default-character-set=latin1 --lock-all-tables --port=3306 --user=root --password=123456 --databases vpdata -R >D://web//App_Data//{0}.sql", "vpdata"); 36 37 //获取mysqldump.exe所在路径 38 String appDirecroty = @"C:/Program Files/MySQL/MySQL Server 5.0/bin/"; 39 40 StartCmd(appDirecroty, command); 41 ScriptManager.RegisterClientScriptBlock(Backup, GetType(), "yes", "alert('数据库已成功备份到D://web//App_Data//Vpdata文件中')", true); 42 } 43 catch (Exception ex) 44 { 45 ScriptManager.RegisterClientScriptBlock(Backup, GetType(), "no", "alert('数据库备份失败!')", true); 46 } 47 } 48 protected void Restore_Click(object sender, ImageClickEventArgs e) 49 { 50 try 51 { 52 53 54 //构建执行的命令 55 String command = string.Format("mysql --host=192.168.1.10 --default-character-set=latin1 --port=3306 --user=root --password=123456 vpdata <D://web//App_Data//{0}.sql", "vpdata"); 56 57 //获取mysql.exe所在路径 58 String appDirecroty = @"C:/Program Files/MySQL/MySQL Server 5.0/bin/"; 59 60 StartCmd(appDirecroty, command); 61 ScriptManager.RegisterClientScriptBlock(Restore, GetType(), "yes", "alert('服务器数据库还原成功!')", true); 62 } 63 catch (Exception ex) 64 { 65 ScriptManager.RegisterClientScriptBlock(Restore, GetType(), "no", "alert('数据库还原失败!')", true); 66 } 67 } 68 69 /// <summary> 70 /// 执行Cmd命令 71 /// </summary> 72 /// <param name="workingDirectory">要启动的进程的目录</param> 73 /// <param name="command">要执行的命令</param> 74 public static void StartCmd(String workingDirectory, String command) 75 { 76 Process p = new Process(); 77 p.StartInfo.FileName = "cmd.exe"; 78 p.StartInfo.WorkingDirectory = workingDirectory; 79 p.StartInfo.UseShellExecute = false; 80 p.StartInfo.RedirectStandardInput = true; 81 p.StartInfo.RedirectStandardOutput = true; 82 p.StartInfo.RedirectStandardError = true; 83 p.StartInfo.CreateNoWindow = true; 84 p.Start(); 85 p.StandardInput.WriteLine(command); 86 p.StandardInput.WriteLine("exit"); 87 }
分类:
ASP.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?