C # back up and restore MySQL database applications

Source: Internet
Author: User

The following articles mainly introduce the practical application of C # backup to restore MySQL database. If you are interested in the practical application of C # backup to restore MySQL database, you can click the following article to learn more about the content.

You can call MySQLdump, a MySQL tool.

Class Cmd to call the cmd command,

The Directory of the process to be started is the directory where MySQL automatically backs up and restores MySQL database tools MySQLdump and MySQL. Of course, this method can be used to execute other command line tools.

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Diagnostics;  
  5. public class Cmd  
  6. {  
  7. / <summary> 
  8.  

/Execute the Cmd command

/</Summary>

/<Param name = "workingDirectory"> directory of the process to be started </param>

/<Param name = "command"> command to be executed </param>

 
 
  1. public static void StartCmd(String workingDirectory, String command)  
  2. {  
  3. Process p = new Process();  
  4. p.StartInfo.FileName = "cmd.exe";  
  5. p.StartInfo.WorkingDirectory = workingDirectory;  
  6. p.StartInfo.UseShellExecute = false;  
  7. p.StartInfo.RedirectStandardInput = true;  
  8. p.StartInfo.RedirectStandardOutput = true;  
  9. p.StartInfo.RedirectStandardError = true;  
  10. p.StartInfo.CreateNoWindow = true;  
  11. p.Start();  
  12. p.StandardInput.WriteLine(command);  
  13. p.StandardInput.WriteLine("exit");  
  14. }  

Backup method:

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.IO;  
  9. using System.Diagnostics;  
  10. using System.Configuration;  
  11. using MDRClient.DataAccess;  
  12. namespace MDRClient  
  13. {  
  14. public partial class DataBackup : Form  
  15. {  
  16. public DataBackup()  
  17. {  
  18. InitializeComponent();  
  19. }  
  20. private void btnBackup_Click(object sender, EventArgs e)  
  21. {  
  22. try  
  23. {  

String command = "MySQLdump -- quick -- host = localhost -- default-character-set = gb2312 -- lock-tables -- verbose -- force -- port = port number -- user = user name -- password = password MySQL database Name-r backup address ";

Build and execute commands
 

 
 
  1. StringBuilder sbcommand = new StringBuilder ();
  2. StringBuilder sbfileName = new StringBuilder ();
  3. SbfileName. appendFormat ("{0}", DateTime. now. toString ()). replace ("-",""). replace (":",""). replace ("","");
  4. String fileName = sbfileName. ToString ();
  5. SaveFileDialog saveFileDialog = new SaveFileDialog ();
  6. SaveFileDialog. AddExtension = false;
  7. SaveFileDialog. CheckFileExists = false;
  8. SaveFileDialog. CheckPathExists = false;
  9. SaveFileDialog. FileName = fileName;
  10. If (saveFileDialog. ShowDialog () = DialogResult. OK)
  11. {
  12. String directory = saveFileDialog. FileName;
  13. Sbcommand. AppendFormat ("MySQLdump -- quick -- host = localhost -- default-character-set = gbk
    -- Lock-tables -- verbose -- force -- port = port number -- user = user name -- password = password Database Name-r \ "{0} \" ", directory );
  14. String command = sbcommand. ToString ();

Path of mysqldump.exe

 
 
  1. String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";  
  2. Cmd.StartCmd(appDirecroty, command); 

MessageBox. Show (@ "MySQL database has been successfully backed up to" + directory + "file", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information );

 
 
  1. }
  2. }
  3. Catch (Exception ex)
  4. {
  5. MessageBox. Show ("database backup failed! ");
  6. }
  7. }
  8. }
  9. }

The restoration method calls MySQL, the built-in MySQL tool. During restoration, note that when the path of the selected file is located, an exception occurs if the file name contains spaces, therefore, double quotation marks ("") are added to the file path name ""

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.IO;  
  9. using System.Diagnostics;  
  10. using System.Configuration;  
  11. using MDRClient.DataAccess;  
  12. namespace MDRClient  
  13. {  
  14. public partial class DataRestore : Form  
  15. {  
  16. public DataRestore()  
  17. {  
  18. InitializeComponent();  
  19. }  
  20. private void btnRestore_Click(object sender, EventArgs e)  
  21. {  

String s = "MySQL -- port = port number -- user = user name -- password = password Database Name <restore file path ";

 
 
  1. try  
  2. {  
  3. StringBuilder sbcommand = new StringBuilder();  
  4. OpenFileDialog openFileDialog = new OpenFileDialog();  
  5. if (openFileDialog.ShowDialog() == DialogResult.OK)  
  6. {  
  7. String directory = openFileDialog.FileName;  

Add "" after the file path to avoid space exceptions.

Sbcommand. appendFormat ("MySQL -- host = localhost -- default-character-set = gbk -- port = port number -- user = username -- password = password MySQL database <\" {0 }\"", directory );

 
 
  1. String command = sbcommand.ToString(); 

Path of mysql.exe obtained

 
 
  1. String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\"; 

DialogResult result = MessageBox. Show ("Do you really want to overwrite the previous database? The previous database data will be lost !!! "," Warning ", MessageBoxButtons. YesNo, MessageBoxIcon. Warning );

 
 
  1. If (result = DialogResult. Yes)
  2. {
  3. Cmd. StartCmd (appDirecroty, command );
  4. MessageBox. Show ("the database is restored successfully! ");
  5. }
  6. }
  7.  
  8. }
  9. Catch (Exception ex)
  10. {
  11. MessageBox. Show ("database restoration failed! ");
  12. }
  13. }
  14. }
  15. }

The above content is an introduction to the MySQL database. I hope you will get some benefits.

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.