I am trying to restore MySQL database data from a dump file, using C# codes.
I am suppose to execute the following command: mysql --verbose --user=root --password=qwerty123456 test < C:\Users\Default\testing.SQL
I know that C# doesn't recognise the "<" symbol so I tried several ways but it still did not work. Can anybody help me with this? I want to restore all my database data back into MySQL using C# codes.
Thanks in advance.
Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe";
process.StartInfo.Argu开发者_StackOverflowments = @"--verbose --user=root --password=qwerty123456 test";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
StreamReader sr = process.StandardOutput;
sr = File.OpenText(@"C:\Users\Default\testing.SQL");
The <
handling should (IIRC) be OK if you simply set UseShellExecute = true
.
However, if you really want to avoid the shell exec, <
is an input - you should be writing the file to StandardInput
. I'l probably leave StandardOutput
alone (set RedirectStandardOutput = false
if you don't actively want the output).
Untested, but maybe:
using(var stdin = process.StandardInput)
using(var reader = File.OpenText(@"C:\Users\Default\testing.SQL")) {
string line;
while((line = reader.ReadLine()) != null) {
stdin.WriteLine(line);
}
stdin.Close();
}
(which should pipe in the file line by line)
精彩评论