2014-10-02 53 views
0

我有一个控制台应用程序,它执行一些命令并输出一些日志。在控制台应用程序中加载cmd

我需要显示命令,并将其结果输出,像这样的:

>Loading the database... 
>mysql -uUser -pPassword myDbName < mydumpFile 
ERROR 1045 (28000): Access denied for user 'User'@'localhost' (using password: Y 
ES) 
>End loading the databse... 

我并如下因素:

void ImportData() 
{ 
    Program.Log("INFO", "Start importing data... <<<"); 

    Process myProcess = new Process(); 
    string mySqlArgs = string.Format(" -u{0} -p{1} {2} < \"{3}\"", 
             bddUser, bddPassword, databaseName, dumpPath); 
    ProcessStartInfo myProcessStartInfo = 
             new ProcessStartInfo("mysql", mySqlArgs); 
    myProcessStartInfo.UseShellExecute = false; 
    myProcessStartInfo.RedirectStandardOutput = true; 
    myProcess.StartInfo = myProcessStartInfo; 
    myProcess.Start(); 

    StreamReader reader = myProcess.StandardOutput; 
    string theOutput = reader.ReadToEnd(); 
    if (theOutput.Length > 0) 
     Program.Log("SQL", theOutput); 

    Program.Log("INFO", "END importing data >>>"); 
} 

但是这个代码

1)不显示命令本身(只是结果)
2)请求也许应该是格式不好,因为结果就像是一个格式错误MySQL命令

更新:新的代码是豆蔻位更好

Program.Log("INFO", "Start importing Materials... <<<".Fill(code)); 

Process cmd = new Process(); 

cmd.StartInfo.FileName = "cmd.exe"; 
cmd.StartInfo.RedirectStandardInput = true; 
cmd.StartInfo.RedirectStandardOutput = true; 
cmd.StartInfo.CreateNoWindow = true; 
cmd.StartInfo.UseShellExecute = false; 

cmd.Start();    

/* execute "mysql -uUser -pPassword base < dump" */ 
string mySqlCommand = "mysql -u{0} -p{1} {2} < \"{3}\""; 
cmd.StandardInput.WriteLine(mySqlCommand, bddUser, bddPassword, databaseName, dumpPath); 
cmd.StandardInput.Flush(); 
cmd.StandardInput.Close(); 

StreamReader reader = cmd.StandardOutput; 
string theOutput = reader.ReadToEnd(); 
if (theOutput.Length > 0) 
    Program.Log("SQL", Environment.NewLine + theOutput); 

Program.Log("INFO", "END importing Materials >>>".Fill(code)); 

,但是无论如何,它显示从cmd.exe的第一执行(之前 MySQL的命令),并且还命令行的附加信息 mysql命令后的结果...

回答

0

执行脚本

mysql -vvv -uUser ... 

此选项将在执行脚本期间显示查询。

至于你的其他问题:你为什么叫一个cmd.exe?直接从你的程序调用mysql.exe并跳过外壳...

+0

如果我直接调用mysql的exe,我不会在输出中看到命令本身,不是吗?就像我没有看到“cmd.exe”实际上... – Serge 2014-10-03 09:34:38

+0

你会看到SQL命令 – Benvorth 2014-10-03 10:41:37

+0

我不需要看到sql命令。我只需要在帖子,命令以及最终的错误(如果不是正确的话)(对于日志)中可以看到的。 – Serge 2014-10-03 11:48:08