1

I found a query from HERE on how to backup the database but it is not working and it throws an error. Any help please. TIA

CODE:

using MySql.Data.MySqlClient;
public bool BackupDatabase(string filename = "")
{
    bool _isExecuted = false;

    if (filename == "")
    {
        filename = string.Format("{0}/db_{1:MMddyyyyHHmmss}.sql", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), DateTime.Now);
    }

    string _queryBackup = string.Format("mysqldump -u '{0}' -p '{1}' '{2}' > '{3}'", DatabaseUsername, DatabasePassword, DatabaseName, filename);         
    using (MySqlConnection con = new MySqlConnection(ConnectionString))
    {
        try
        {
            con.Open();
            MySqlCommand cmd = new MySqlCommand(_queryBackup, con);

            cmd.ExecuteNonQuery();
            _isExecuted = true;
        }
        catch (Exception ex)
        {
            string _x = ex.Message;
        }
    }

    return _isExecuted;
}

ERROR:

_x contains this error

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump -u 'root' -p 'usbw' 'my_database_name' > 'C:\Users\DesktopName\Desktop\db_08242012164022.sql' at line 1"

1

1 Answer 1

1

Your _queryBackup is not an sql query string but a string to run from the system, as if you run it from the cmd console.

Here is some code to run a command such as yours from C#

  var startInfo = new ProcessStartInfo(command, parameterstring);
  startInfo.RedirectStandardError = true;
  startInfo.CreateNoWindow = true;
  startInfo.RedirectStandardOutput = true;
  startInfo.UseShellExecute = false;
  startInfo.ErrorDialog = false;
  Process process = Process.Start(startInfo);
  StreamReader stdoutSR = process.StandardOutput;
  string stdout = stdoutSR.ReadToEnd();
  StreamReader stderrSR = process.StandardError;
  string stderr = stderrSR.ReadToEnd();
Sign up to request clarification or add additional context in comments.

1 Comment

ahhh.. so, is there any sql query string to backup the database? so the given method is applicable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.