我正在尝试备份mysql数据库。我创建的代码是
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();问题是当数据库大小很小时,它可以正常工作,但当我试图备份大型数据库(大约800MB )时,我得到了Out Of Memory Exception。
发布于 2012-12-06 15:03:31
与其在C#中读取标准输出,为什么不直接通过命令行将其写入文件呢?
我通常使用以下命令获取mysqldump。它可以在Windows和Linux上运行。
mysqldump -u{user} -p{password} --routines --triggers --result-file={dest_filename} {dbname}-或者-
mysqldump -u{user} -p{password} --routines --triggers {dbname} > {dest_filename}您遇到的Out of memory异常可能是在尝试将mysqldump的整个输出读取到C#中的以下行的内存中时引起的(通常在字符串超过一定大小时发生)。
res = p.StandardOutput.ReadToEnd();发布于 2012-12-06 15:40:00
看起来很大的线吃掉了内存。
尝试使用OutputDataReceived事件在输出文件中写入数据。
这里有两个链接和示例-
https://stackoverflow.com/questions/13738361
复制相似问题