2016-11-03 10 views
3

sqlcmdを使用してC#コードで自動的に実行しようとしている、非常に大きな複数のsqlファイルがあります。 ProcessStartInfoを使用してコマンドプロンプトを起動し、そこからsqlcmdを実行します。ただし、sqlcmdが最初のファイルの実行を終了すると、コマンドウィンドウは終了し、アプリケーションは「Wait for Exit」でハングアップします。 SQLファイルを実行した後、コマンドプロンプトが終了するようにするにはどうすればよいですか?コマンドプロンプトからc#で実行が終了した後にsqlcmdを閉じます

for (int i = 1; i <= fileCount; i++) 
{ 
    string readFile = fileToRead + "_" + i + ".sql"; 
    string writeFile = fileToWrite + "_" + i + ".txt"; 

    string commandString = "sqlcmd -S myservername -d mydatabasename -i " + readFile + " -o " + writeFile + " -a 32767"; 

    ProcessStartInfo ProcessInfo; 
    ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + commandString); 
    ProcessInfo.CreateNoWindow = true; 
    ProcessInfo.UseShellExecute = false; 
    ProcessInfo.RedirectStandardOutput = true; 

    using (Process Process = Process.Start(ProcessInfo)) 
    { 
     Process.WaitForExit(); //hangs here because window stays open, have to manually exit it to continue 
    } 
} 
+3

次の行に/ Cで/ Kを交換して試してみてください/ Kの代わりに/ Cを使うと、コマンドプロンプトとタイプインを開くことができますCMD.EXE /? – Steve

+0

実行しているプロセスで、ユーザーの入力を待っていますか? –

答えて

1

D:\>help cmd 
Starts a new instance of the Windows command interpreter 

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V: 
    [[/S] [/C | /K] string] 

/C  Carries out the command specified by string and then terminate 
/K  Carries out the command specified by string but remains 
/S  Modifies the treatment of string after /C or /K (see below) 
.... 

ですから、このオプションを指定している:

/K  Carries out the command specified by string but remains 
1

ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + commandString); 
関連する問題