C # MySQL database backup and restoration Initialization

Source: Internet
Author: User

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.

 

 

 

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.