2010-12-15 145 views
3

我正在尝试使用C#代码从转储文件恢复MySQL数据库数据。C#MySQL数据库恢复

我想执行以下命令: MySQL的--verbose --user =根--password = qwerty123456测试< C:\用户\默认\ testing.SQL

我知道C#没有按”不承认“<”符号,所以我尝试了几种方法,但仍然无效。任何人都可以帮助我吗?我想使用C#代码将所有数据库数据恢复到MySQL中。

在此先感谢。

  Process process = new Process(); 
      process.StartInfo.FileName = @"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"; 
      process.StartInfo.Arguments = @"--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"); 

回答

2

<处理应(IIRC)是确定的,如果你简单地设置UseShellExecute = true

但是,如果你真的想避免外壳EXEC,<输入 - 你应该将文件写入StandardInput。我可能单独留下StandardOutput(如果您不主动需要输出,请设置RedirectStandardOutput = false)。

未经检验的,但也许:

 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(); 
     } 

(应该由行的文件行管)

+0

谢谢!有用。 – athgap 2010-12-15 08:16:39