2

I am trying to take back up of a mysql database. the code i am created is

ProcessStartInfo proc = new ProcessStartInfo();
string cmd = string.Format(@"-u{0} -p{1} -E -R -h{2} {3}", UserName, PWD, hostname, dbname);
proc.FileName = "Path to mysqldump.exe";
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = true;
proc.Arguments = cmd;
proc.UseShellExecute = false;
Process p = Process.Start(proc);
string res;
res = p.StandardOutput.ReadToEnd();
file.WriteLine(res);
p.WaitForExit();
file.Close();

The problem is it working correctly when the database size is small but i am getting Out Of Memory Exception when i am trying to take back up of large database (Approximately 800 MB ).

3
  • 1
    Error message explains many things.How much space do you have in C drive determine the space for backup operations as the temp memory is used while taking backups or any curd memory management. Commented Dec 6, 2012 at 6:55
  • @Pratik About 64 GB free space is there.. Commented Dec 6, 2012 at 6:57
  • Method to read the database file can u post sample as this could be the reason Commented Dec 6, 2012 at 7:11

2 Answers 2

1

Instead of reading the standard output in C# why don't you write it directly to file through the command line?

I typically take my mysqldumps using the following command. It works in both Windows and Linux.

mysqldump -u{user} -p{password} --routines --triggers --result-file={dest_filename} {dbname}

-OR-

mysqldump -u{user} -p{password} --routines --triggers {dbname} > {dest_filename}

The Out of memory exception you encountered would probably have been caused when trying to read the entire output of mysqldump to memory in C# on the following line (typically happens when a string exceeds beyond a certain size).

res = p.StandardOutput.ReadToEnd();
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Tanzeel let me try.
I think ">" is for dos command it giving me error "couldn't find table >"
@Vijesh Yes that's right as I stated in the answer through the command line. You can use the first suggestion using --result-file parameter or you can run it via cmd /c "{command to run}" as well.
@Rakesh can u share the code... i am also looking for similar solution
0

Looks like very big string ate the memory.

Try to use OutputDataReceived event to write data in output file.

Here are two links with examples -

  1. Process.OutputDataReceived Event
  2. Process.BeginOutputReadLine Method

Comments

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.