471,330 Members | 1,150 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Having problems running console command with parameters

Hi,
I'm trying to run mysqldump.exe from my c# application, running on windows
vista with visual studio 2005.
The following code does not produce the output file. The same line used in a
command prompt works fine.
While debugging, I get an exception "InvalidOperationException, StandardOut
has not been redirected or the process hasn't started yet." when attempting
the System.Console.Write(p.StandardOutput.ReadToEnd() line.
I get no exception but an output line of "mysqldump: Got errno 9 on write"
when attempting the System.Console.Write(p.StandardError.ReadToEnd())
command.
If I use a largish database and dont turn on the redirections, I appear to
get the output to the console rather than to file, but the text flashes by
so fast I can't read what's on the first few lines.
I don't know if it's something Im doing wrong, or vista. Is anyone able to
help please.

mysqldumpstring = @"--user=root --password=masterkey --hex-blob --databases
test c:\temp\claire_dump.sql";

Process MySQLDump = new Process();
ProcessStartInfo info = new ProcessStartInfo("mysqldump.exe");// mysqldump
is in environment path
info.Arguments = mysqldumpstring;
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardError = true;

Process p = Process.Start(info);
p.WaitForExit();

try
{
System.Console.Write(p.StandardOutput.ReadToEnd()) ;
}
catch { }
try
{
System.Console.Write(p.StandardError.ReadToEnd());
}
catch { }

Jun 27 '08 #1
1 4583
The problem I had was caused by mysqldump getting confused by the section of
the argument string sending redirection of standard input to a file. This
meant that I needed to remove the output file name from the argument list.
As my database can become huge, I wanted to redirect standard output to file
rather than using the default memory stream.
My clever friend helped me with the following code showing redirection to
file.

StreamWriter OutputStream;

private void OnDataReceived(object Sender, DataReceivedEventArgs e)
{
// Write each line to the output file
if (e.Data != null)
OutputStream.WriteLine(e.Data);
}

public void BackupLocalDatabase(DatabaseHandler Handler, string
OutputFilename)
{
string fnName = "BackupLocalDatabase";
try
{
OutputStream= new StreamWriter(OutputFilename);
try
{
// Need to use "--databases" to force CREATE DATABASE command in
script
// --hex-blob to turn blob fields into hex strings
string mysqldumpstring = string.Format(" --databases
{0} --user={1} --password={2} --hex-blob",
Handler.DatabaseName, Handler.UserID, Handler.Password);

// Create info needed by process
ProcessStartInfo info = new ProcessStartInfo("mysqldump");
info.Arguments = mysqldumpstring;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;

// Create process
Process p = new Process();
p.StartInfo = info;
// Set up asynchronous read event
p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();

//TODO, check for errors

}
finally
{
// Flush and close file
OutputStream.Flush();
OutputStream.Close();
}
}
catch (Exception e)
{
LogException(fnName, e);
throw e;
}

}// function

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Hal Vaughan | last post: by
1 post views Thread by Michael Hetrick | last post: by
7 posts views Thread by Samantha Penhale | last post: by
3 posts views Thread by Steve Le Monnier | last post: by
17 posts views Thread by Csaba Gabor | last post: by
3 posts views Thread by Nicol | last post: by
3 posts views Thread by Alex | last post: by
1 post views Thread by =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.