Problem, executing commands in cmd using c# -
I need to execute the command at the command prompt.
C: MySQL \ MySQL Server 5.0 \ bin> mysql -uroot-popassword & lt; D: /admindb/aar.sql
When I do this manually in CMD, I am getting my results.
Now I am trying to do this program by executing the C # code in CMD.
I'm using the code below to do this. I'm not getting any errors and results !!!
When I debug, I get the value of the string command line below,
"\" C: \\ MySQL \\ MySQL Server 5.0 \\ bin \\\ "-uroot -ppassword> \" D: /admindb/AAR12.sql "
I think the problem is with this string, passed .. cmd it ?? solution to how to
public void execute () {string Commands = "\" "+ MySqlCommandPath +" \ "" + "-u" + DbUid + "- p" + DBWW + "& gt; "+" \ "" + Path.combin (pathbackup, flankname backup + excite + ".sql"); System.Diagnostics.ProcessStartInfo PSI = New System Diagnostics. Process StartInfo ("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 (command line); SW.Close (); } I used one more object-oriented impulse for the same task. I read the file using a StreamReader and use the console redirection to inject the text directly into the mysql exe file. works like a charm.
UPDATE: Here is the code, it is not VB, but it should be easy to translate. I use this code to make empty db for some unit tests. The only advantage of starting mysql from a commandline with a file in the form of input is that
a) I make some exception if I create an exception - b) You can use this code easily without A real file C) If the exception is thrown into a test project, then you get error output directly from mysql in your test protocol.
Please note that user names, passwords, schemas, have merged properties in my class, you can change them with string or modify the function call.
Public Shared Sub SetupDatabase () mysql_binary dim String = "mysql.exe" dim mysql_args string = String.Format ( "initial_db.sql to as the" .. \ .. \ .. \ MyProject \ "'Basic Configuration String = - h {0} -U {1} -p {2} {3}", hostname, username, password, schema) as slow infile' New Stream Reader (Infel ) as a stream reader Infil Daimary Reader 'P.StartInfo.FileName = mysql_binary p.StartInfo.Arguments = mysql_args' redirect input / output / Stederar p. StartInfo.ReadreadsteadOutput = True P. Startinfo Redirect Stratored Errator = True P. Startinfo Redireststeeninput = Truce p. Startinfo Use Selaksakyut = Fail P .StartInfo.CreateNoWindow = True p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 'Start process can read p.Start ()' infile and what passed for MySQL reader.EndOfStream = false p.StandardInput .WriteLine (reader.ReadLine) loop while close stdin p.StandardInput.Close () 'stdout / stderr = p.StandardOutput.ReadToEnd (as read by the dim stdout string) dim stderr string = p.StandardError.ReadToEnd () Wait for 'mysql as p.WaitForExit () to stop' if exitcode! = 0 then take an exception and are included in the output from stderr if p.ExitCode & lt; & Gt; (: & Amp; vbNewLine & amp; stderr ""; & amp "failed with the initial DB setup Exitcode"; p.ExitCode & amp) finally p.Close () reader.Close () End Sub
< 0 then throw a new exception / pre>
Comments
Post a Comment