ASP. NET Backup Restore MySQL database dump

Source: Internet
Author: User
1    Protected   Void Page_load ( Object  Sender, eventargs E)  2   {  3   Uilogic. Sort AchE ();  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 Server 5.0 // data // vpdata  " ); //  Create a vpdata database in the MySQL server 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  " ; //  Backup File Name and Path  28               //  Determine whether a directory exists  29               If (! System. Io. file. exists (filename ))  30   {  31   System. Io. Directory. createdirectory (filepath );  32  }  33   34               //  Build and execute commands  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               //  Path of mysqldump.exe  38 String appdirecroty = @"  C:/program files/MySQL Server 5.0/bin/  "  ;  39   40   Startcmd (appdirecroty, command );  41 Scriptmanager. registerclientscriptblock (backup, GetType (),"  Yes  " , "  Alert ('the database has been successfully backed up to the D: // web // app_data // vpdata File ')  " , True  );  42   }  43           Catch  (Exception ex)  44   {  45 Scriptmanager. registerclientscriptblock (backup, GetType (), "  No  " , "  Alert ('database backup failed! ')  " , True  );  46   }  47   }  48       Protected   Void Restore_click (Object  Sender, imageclickeventargs E)  49   {  50           Try  51   {  52   53   54               //  Build and execute commands  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               //  Path of mysql.exe obtained  58 String appdirecroty = @"  C:/program files/MySQL Server 5.0/bin/  " ;  59   60   Startcmd (appdirecroty, command );  61 Scriptmanager. registerclientscriptblock (restore, GetType (), "  Yes  " , "  Alert ('server database restored successfully! ')  " , True  );  62   } 63           Catch  (Exception ex)  64   {  65 Scriptmanager. registerclientscriptblock (restore, GetType (), "  No  " , "  Alert ('database restoration failed! ')  " , True  );  66  }  67   }  68   69       ///   <Summary>  70       ///  Run the CMD command  71       ///   </Summary>  72       ///   <Param name = "workingdirectory">  Directory of the process to be started </Param>  73       ///   <Param name = "command">  Command to be executed  </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 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.