I need to execute the below command in command prompt.
C:\MySQL\MySQL Server 5.0\bin>mysql -uroot -ppassword < d:/admindb/aar.sql
When i do this manually in cmd, i am getting my results.
Now i am trying to do this programatically, to execute it in cmd from c# code.
I am using the below code to do it. I am not getting any errors and Result !!!
When i debug, i get the value of string commandLine as below,
"\"C:\\MySQL\\MySQL Server 5.0\\bin\\\" -uroot -ppassword > \"D:/admindb/AAR12.sql"
I guess the problem is with this string, passed to cmd. How to solve this ??.
public void Execute()
{
string commandLine = "\"" + MySqlCommandPath + "\"" + " -u" + DbUid + " -p" + DbPwd + " > " + "\"" + Path.Combine(Path_Backup, FileName_Backup + ExcID + ".sql");
System.Diagnostics.ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo("cmd.exe");
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(PSI);
System.IO.StreamWriter SW = p.StandardInput;
System.IO.StreamReader SR = p.StandardOutput;
SW.WriteLine(commandLine);
SW.Close();
}
I used a more object oriented approch for the same task.
I read the file using a StreamReader and use console redirection to directly injecting the text to the mysql exe file. Works like a charm.
Update: Here is the code, it’s VB not C# but should be easy to translate.
I use this code to create an empty db for some unit tests. The advantage over just starting mysql from the commandline with an file as input is that
a) I create an exception if something fails
b) You could easily use this code to control mysql without needing a real file.
c) In a TestProject if the Exception is thrown you get the error output from mysql directly in your TestProtocol.
Please note that UserName, UserName, Password, Schema are Shared Properties in my class, you can just replace them with strings or modify the function call.