0

I'm writing an application which should make a complete copy of one database and then import it on the same server with different name.
So, I guess I should use mysqldump and mysql and the parameters that I should pass to them.
Okay, but I can't get the dump put the file where I want, because I have to know the location and then pass it to mysql.

StringBuilder exportPath = new StringBuilder();
//exportPath.Append(Directory.GetCurrentDirectory());
exportPath.Append(@" > C:\Temp\backup.sql");

Process MySQLDump = new Process();
MySQLDump.StartInfo.UseShellExecute = true;
//MySQLDump.StartInfo.RedirectStandardOutput = true;
MySQLDump.StartInfo.FileName = "mysqldump";
MySQLDump.StartInfo.Arguments = "-u root -proot -h localhost mytable" + exportPath;
MySQLDump.Start();
//string theDump = MySQLDump.StandardOutput.ReadToEnd();
MySQLDump.WaitForExit();
MySQLDump.Close();

I'm doing something wrong, but I don't know what.

Paul Reed
  • 133
  • 2
  • 10
  • does the mysqldump command work from the command line as constructed? Does the user running your c# have permissions to run mysqldump? – mconlin May 10 '13 at 13:52
  • Try to put a 1 in front of > like `1>C:\\......` – Steve May 10 '13 at 13:58
  • When I'm running the exact same command in the CMD everything works, but when I'm runing my project nothing happens. – Paul Reed May 10 '13 at 14:09

1 Answers1

2

You have two choices:

  • Do not redirect the output of mysqldump in the command line, but use a more verbouse process creation and hook the standard output of mysqldump. This makes ist possible to postprocess the file (Hash it as an example) and either write it out to where you want, or run it directly into the standard input of the importing instance.
  • Understand, that > C:\\Documents and Settings\\admin\\Desktop\\databases\\db.sql is not a command line parameter. You need to sssemble and the parameter and redirection part string and use shell execution. Make sure, you use an absolute path name.
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • I'm kinda newbie in C# programmning, so sorry if I'm asking stupid questions. :D How to do that? I too was thinking to bypass the saving to file process, but I don't know how to send it back to mysql. And what if the mysql export is big like 300-400 megabytes? – Paul Reed May 10 '13 at 14:10
  • You would want to look at the `ProcessStartInfo` class - create an instance, fill it out and make sure you set `RedirectStandardOutput`. Create a process from it, after it has started, use `Process.StandardOutput` to read the output. You could read into memory until a threshold (e.g. 100MB), if you exceed this use a temporary file. – Eugen Rieck May 10 '13 at 14:32
  • Thank you! I readed about Processstart and tried to pass the data between the processes. Everything works fine, but the memory limit bothers me. 100mb is not enough for my purposes. Is there a way to extend the limit, because doing it that way looks more handy to me? If this cannot be done, how to assemble the file path? I tried to manualy write the url like in the first post, then tried directly with `GetCurrentDirectory` method, but it seems like I cannot use it in the argument parameter. – Paul Reed May 13 '13 at 07:37
  • Just use either a `StringBuilder` or simply the `+` operator to assemble the output path into a single string, then do not forget to put it in **quotes** - a path like "Documents and Setting" contains spaces and will thus need the quotes. – Eugen Rieck May 13 '13 at 07:53
  • I'm making a huge mistake somewhere, but I dont know where. No matter how I build the temporary file path when I concatenate it with the Argument method of processinfo mysqldump returns error and I'm seeing that it doesnt take the path as path for saving the file, but it takes it as database name. `mysqldump: couldnt find table ">"`. Can you tell me where Im I wrong? Otherwise I tried another way: Im saving the output from the mysqldump and then with `File.WriteAllText` I'm saving it to a file, but another stone: how to pass the file to the mysql process and is this method good? – Paul Reed May 13 '13 at 08:17
  • Did you forget the shell execute? If you forget it, the redirection will be passed to mysqldump, which will not like it ... – Eugen Rieck May 13 '13 at 08:44
  • I updated the code in my first post, can you please take a look? :) – Paul Reed May 13 '13 at 08:55
  • Try `MySQLDump.StartInfo.FileName = "cmd"; MySQLDump.StartInfo.Arguments = "/c mysqldump -uroot -proot -h localhost mytable" + exportPath;` – Eugen Rieck May 13 '13 at 09:55
  • Yep. Thank you! I guess starting the cmd as process is the only way. :) – Paul Reed May 13 '13 at 10:05