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.
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- public class Cmd
- {
- / <summary>
-
/Execute the Cmd command
/</Summary>
/<Param name = "workingDirectory"> directory of the process to be started </param>
/<Param name = "command"> command to be executed </param>
- public static void StartCmd(String workingDirectory, String command)
- {
- Process p = new Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.WorkingDirectory = workingDirectory;
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- p.StandardInput.WriteLine(command);
- p.StandardInput.WriteLine("exit");
- }
- }
Backup method:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- using System.Configuration;
- using MDRClient.DataAccess;
- namespace MDRClient
- {
- public partial class DataBackup : Form
- {
- public DataBackup()
- {
- InitializeComponent();
- }
- private void btnBackup_Click(object sender, EventArgs e)
- {
- try
- {
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
- StringBuilder sbcommand = new StringBuilder ();
- StringBuilder sbfileName = new StringBuilder ();
- SbfileName. appendFormat ("{0}", DateTime. now. toString ()). replace ("-",""). replace (":",""). replace ("","");
- String fileName = sbfileName. ToString ();
- SaveFileDialog saveFileDialog = new SaveFileDialog ();
- SaveFileDialog. AddExtension = false;
- SaveFileDialog. CheckFileExists = false;
- SaveFileDialog. CheckPathExists = false;
- SaveFileDialog. FileName = fileName;
- If (saveFileDialog. ShowDialog () = DialogResult. OK)
- {
- String directory = saveFileDialog. FileName;
- 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 );
- String command = sbcommand. ToString ();
Path of mysqldump.exe
- String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
- Cmd.StartCmd(appDirecroty, command);
MessageBox. Show (@ "MySQL database has been successfully backed up to" + directory + "file", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information );
- }
- }
- Catch (Exception ex)
- {
- MessageBox. Show ("database backup failed! ");
- }
- }
- }
- }
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 ""
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- using System.Configuration;
- using MDRClient.DataAccess;
- namespace MDRClient
- {
- public partial class DataRestore : Form
- {
- public DataRestore()
- {
- InitializeComponent();
- }
- private void btnRestore_Click(object sender, EventArgs e)
- {
String s = "MySQL -- port = port number -- user = user name -- password = password Database Name <restore file path ";
- try
- {
- StringBuilder sbcommand = new StringBuilder();
- OpenFileDialog openFileDialog = new OpenFileDialog();
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- 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 );
- String command = sbcommand.ToString();
Path of mysql.exe obtained
- 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 );
- If (result = DialogResult. Yes)
- {
- Cmd. StartCmd (appDirecroty, command );
- MessageBox. Show ("the database is restored successfully! ");
- }
- }
-
- }
- Catch (Exception ex)
- {
- MessageBox. Show ("database restoration failed! ");
- }
- }
- }
- }
The above content is an introduction to the MySQL database. I hope you will get some benefits.