Backup and Restore a MySQL Database in C#.NET

You can find the same article in my dotnetspider article from the following link

Backup and Restore a MySQL Database in C#.NET

The code sample automates the process of backing up and restoring a MySQL database.
Create two batch files separately for backup(MySql_Backup.bat) and restore(MySql_Restore.bat).

@ECHO off
set DBchoice=%1%
set User=%2%
set Password=%3%
set pathchoice=%4%

@REM Remove double quotes from the path
@REM SET pathchoice=%pathchoice:"=%
@REM SET pathchoice=%pathchoice:"=%

mysqldump --add-drop-table -B %DBchoice% -u %User% --password=%Password%  > %pathchoice%


@ECHO off

set User=%1%
set Password=%2%
set DBchoice=%3%
set pathchoice=%4%
set hostIP=%5%
set toolPath=%6%

@REM Remove double quotes from the path
@REM SET pathchoice=%pathchoice:"=%
@REM SET pathchoice=%pathchoice:"=%

%toolPath%\mysql -u %User% -h%hostIP% --password=%Password% %DBchoice% < %pathchoice%

Create the following method to execute the batch files with proper parameters

/// <summary
    /// Author : Himasagar Kutikuppala
    ///A utility method that runs the batch file with supplied arguments.
    /// </summary 
    /// <param name="batchFileName">Name of the batch file that should be run</param>;
    /// <param name="argumentsToBatchFile">Arguments to the batch file</param>;
    /// <returns>;Status of running the batch file</returns>;
    protected bool ExecuteBatchFile(string batchFileName, string[] argumentsToBatchFile)
        string argumentsString = string.Empty;
            //Add up all arguments as string with space separator between the arguments
            if (argumentsToBatchFile != null)
                for (int count = 0; count < argumentsToBatchFile.Length; count++)
                    argumentsString += " ";
                    argumentsString += argumentsToBatchFile[count];
                    //argumentsString += "\"";
            //Create process start information
            System.Diagnostics.ProcessStartInfo DBProcessStartInfo = new System.Diagnostics.ProcessStartInfo(batchFileName, argumentsString);

            //Redirect the output to standard window
            DBProcessStartInfo.RedirectStandardOutput = true;

            //The output display window need not be falshed onto the front.
            DBProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            DBProcessStartInfo.UseShellExecute = false;

            //Create the process and run it
            System.Diagnostics.Process dbProcess;
            dbProcess = System.Diagnostics.Process.Start(DBProcessStartInfo);

            //Catch the output text from the console so that if error happens, the output text can be logged.
            System.IO.StreamReader standardOutput = dbProcess.StandardOutput;

            /* Wait as long as the DB Backup or Restore or Repair is going on. 
            Ping once in every 2 seconds to check whether process is completed. */
            while (!dbProcess.HasExited)

            if (dbProcess.HasExited)
                string consoleOutputText = standardOutput.ReadToEnd();
                //TODO - log consoleOutputText to the log file.
            return true;
        // Catch the SQL exception and throw the customized exception made out of that
        catch (SqlException ex)

            throw SQLExceptionClassHelper.GetCustomMsSqlException(ex.Number);
        // Catch all general exceptions
        catch (Exception ex)
            throw new CustomizedException(ARCExceptionManager.ErrorCodeConstants.generalError, ex.Message);

What is GST

GST (Goods and Service Tax) is a comprehensive tax on the supply of goods and services at each stage of any transaction. Read More

Income Tax Information

An income tax is a tax imposed by government on income earned by you. Income tax is a key source of funds that the government uses to fund its activities and serve the public. Read More

General Insurance Companies in India

General insurance is insurance for valuables other than our life and health. General insurance covers the insurer against damage, loss and theft of your valuables. Read More

Types of Bank Loans In India

Loan means lending money from one individual or entity to another. A loan has three components – principal or the borrowed amount, rate of interest and tenure or duration for which the loan is availed. Read More

List of Banks in India

The Reserve Bank of India is the central Bank that is fully owned by the Government of India. It is governed by a central board (headed by a Governor) appointed by the Central Government. Read More

List of Educational Institutions in India

The following list of comprehensive websites on higher education in India. These websites will provide detailed information on education system in India. Read More