C # tip-C # back up and restore the MySQL database

Source: Internet
Author: User
It is better to explain the classification of the database separately. However, as a skill, it is hard to determine how to classify the database. Backup method:First, obtain the installation path of the MySQL program through the registry. If it is version 5.0, it is:

var registerLocation = @"Software\MySQL AB\MySQL Server 5.0"; 
var key = Registry.LocalMachine.OpenSubKey(registerLocation);
if (key != null)
{
var location = key.GetValue("Location").ToString();
}
After obtaining the path, create a new process and prepare to start the backup. The specific details are annotated with the code.

VaR proc = new process ();
// Use the following command line to run cmd.exe
Proc. startinfo. filename = "cmd.exe ";
// Disable System Shell startup
Proc. startinfo. useshellexecute = false;
// Set the working directory to location + @ "bin"
// Because mysqldump is used for backup, and mysqldump is in the directory location + @ "bin"
Proc. startinfo. workingdirectory = location + @ "bin ";
// For mysqldump parameters, refer to the notes on the MySQL official website. The format method is used here.
// What does each of the obtained command parameters mean? It is literally easy to understand, not to explain, ^_^
Proc. startinfo. arguments = string. format ("mysqldump.exe -- default-character-set = utf8-H {0}-U {1}-P {2} {3 }>{ 4}", server, user, PWD, database, backupfile );
// Do not create a window, which is equivalent to hiding the interface
Proc. startinfo. createnowindow = true;
// The following two sentences are used for debugging purposes.
Proc. startinfo. redirectstandardinput = true;
Proc. startinfo. redirectstandardoutput = true;
// Start the backup.
Proc. Start ();
// Wait until the backup is complete
Proc. waitforexit ();
// Close the process
Proc. Close ();
The backup is completed in a few steps. Restoration method:In the following example, only mysql.exe needs to be modified. The specific parameter-force indicates forced restoration. Others are clear and not explained.

proc.StartInfo.Arguments = string.Format("mysql.exe --force -h {0} -u {1} -p{2} {3} < {4}",
server,user,pwd,database,restoreFile);
No extra code is written. O (nobody _ Nobody) O Haha ~

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.