Main functions:
1. Database Initialization
2. Database Backup | restore
The Code is as follows:
// Execute the create database operation this. getexecute (g_con, "create database if not exists newdb"); this. sqladdress = "-h" + IP + "-U" + User + "-P" + password + "newdb"; // backup private void btn_dump_click (Object sender, eventargs e) {using (savefiledialog SFD = new savefiledialog () {SFD. filter = "database file | *. SQL "; SFD. filterindex = 0; SFD. restoredirectory = true; SFD. filename = "backup" + datetime. now. tostring ("yyyymm Ddhhmmss ") + ". SQL "; if (SFD. showdialog () = system. windows. forms. dialogresult. OK) {string filepath = SFD. filename; string cmd = "mysqldump" + sqladdress + "> \" "+ filepath +" \ ""; string result = runcmd (CMD); If (result. trim () = "") {MessageBox. show ("Database Backup successful! "," CMS ", messageboxbuttons. OK, messageboxicon. information);} else {MessageBox. show (result, "CMS", messageboxbuttons. OK, messageboxicon. information) ;}}}// restore the database // restore the database private void btn_import_click (Object sender, eventargs e) {If (this. tb_path.text.trim () = "") {MessageBox. show ("select the file to restore! "," CMS ", messageboxbuttons. OK, messageboxicon. information); return;} // This. getexecute (g_con, "create database if not exists clothes"); string filepath = This. tb_path.text.trim (); string cmd = "MySQL" + sqladdress + "<\" "+ filepath +" \ ""; string result = runcmd (CMD); If (result. trim () = "") {MessageBox. show ("database recovery successful! "," CMS ", messageboxbuttons. OK, messageboxicon. information);} else {MessageBox. show (result, "CMS", messageboxbuttons. OK, messageboxicon. information) ;}/// command line operation private string runcmd (string command) {// process PROCESS p = new process (); p. startinfo. filename = "cmd.exe"; // determine the program name p. startinfo. arguments = "/C" + command; // determine the program command line P. startinfo. useshellexecute = false; // use p. startinfo. redirectstandardinput = true; // redirect input p. startinfo. redirectstandardoutput = true; // redirect the output p. startinfo. redirectstandarderror = true; // redirect Output Error p. startinfo. createnowindow = true; // set to not display the window p. start (); // 00 p. standardinput. writeline (command); // you can enter the command p. standardinput. writeline ("exit"); // you must add exit or the next program // P. waitforexit (); // P. close (); // return p. standardoutput. readtoend (); // output the stream to obtain the command line result. Return p. standarderror. readtoend ();}
The main principle is to use MySQL commands mysqldump and MySQL for export and import operations, in which the Windows command line is called.
At the beginning, I used navicat for MySQL to back up the database. When I restored the database, I found that one machine was normal and one machine went wrong. I was depressed. Later I found that it should be
The MySQL command of this tool is different from the MySQL Command version. The format of the data file content exported by this tool is different from that exported by the program.
When all files exported by the program are used as restoration files, both machines are normal. However, navicat for MySQL is recommended. Multi-type database datastudio is king!
NOTE: If there is no command error, add the MySQL installation path in the system environment variable, for example, c: \ Program Files \ mysql server 5.5 \ bin.