0

I've got a code to create a backup copy from a query, this query takes the older registers from a year in the past and saves them into a *.sql file. But the problem that I found is when I open the .sql file and it's empty. I've tried the query in command prompt and it works perfectly.

I'm new using mysql and I have read these posts to create my code (and looking for answers): Backing up Database in MySQL using C#

C# and mysqldump

How to take backup of MySQL Database

try
{
    string strBackupFileName = GetBackUpFileName();
    StreamWriter strBackupFile = new StreamWriter(strBackupFileName);

    ProcessStartInfo psInfo = new ProcessStartInfo();
    psInfo.FileName = @"c:\Users\current.user\source\xampp\mysql\bin\mysqldump.exe";
    psInfo.RedirectStandardInput = false;
    psInfo.RedirectStandardOutput = false;
    psInfo.Arguments = "- u root -h localhost --databases --hex-blob -n -t dashboard --tables dashboard.backup --where='updated_at < NOW() - INTERVAL 365 DAY'";
    psInfo.UseShellExecute = false;
    psInfo.RedirectStandardOutput = true;
    Process backup_process = Process.Start(psInfo);

    string stdout;
    stdout = backup_process.StandardOutput.ReadToEnd();
    strBackupFile.WriteLine(stdout);
    backup_process.WaitForExit();
    strBackupFile.Close();
    backup_process.Close();
    MessageBox.Show("Backup done at file:" + strBackupFileName);
}
catch (Exception ex)
{
    MessageBox.Show("Error during the backup: \n\n" + ex.Message);
}

When I run the program I see that mysqldump doesn't "wait" to do the process (when I do it manually, it takes at least 25-30 seconds), it opens a command prompt window and closes immediately.

SimonG
  • 312
  • 7
  • 20

1 Answers1

0

The solution was simple, in: psInfo.Arguments = "- u root -h localhost --databases --hex-blob -n -t dashboard --tables dashboard.backup --where='updated_at < NOW() - INTERVAL 365 DAY'"; only i must to delete the space between "-" and "u".