ASP.NET備份還原MySql資料庫 Dump

來源:互聯網
上載者:User
 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         else12         {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         try23         {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('Database Backup失敗!')", true);46         }47     }48     protected void Restore_Click(object sender, ImageClickEventArgs e)49     {50         try51         {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     }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.