2016-11-03 9 views
3

sqlcmd를 사용하여 C# 코드에서 자동으로 실행하려고하는 매우 큰 여러 sql 파일이 있습니다. ProcessStartInfo를 사용하여 명령 프롬프트를 시작하고 거기에서 sqlcmd를 실행합니다. 그러나 sqlcmd가 첫 번째 파일 실행을 마친 후에는 명령 창이 계속 나타나고 "Wait for Exit"때 응용 프로그램이 중단됩니다. 내 명령 프롬프트가 SQL 파일을 실행 한 후에 종료되도록하려면 어떻게해야합니까?C#에서 실행을 마치면 sqlcmd를 닫습니다.

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) 
.... 

은 그래서 당신은이 옵션을 지정한 : 명령 프롬프트에서

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

/K 대신/C를 사용하면 명령 프롬프트와 typi를 열 수 있습니다. CMD.EXE /? – Steve

+0

실행중인 프로세스가 사용자 입력을 기다리고 있습니까? –

답변

1

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

다음 줄에/C에 의해/K를 교체 시도

ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + commandString);