Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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);
}


What I have tried:

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
Posted
Updated 30-May-19 22:13pm

1 solution

C#
psInfo.Arguments = "- u root -h localhost --databases --hex-blob -n -t dashboard --tables dashboard.backup --where='updated_at < NOW() - INTERVAL 365 DAY'";

Maybe that space between the - character and the u is causing problems.
 
Share this answer
 
Comments
Member 14471936 31-May-19 4:44am    
OMG that was the problem!, thank you so much!!!
[no name] 18-Jun-19 10:53am    
thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900