c#执行完后关闭sqlcmd

Close sqlcmd after finishing executing in c#

我有多个非常大的 sql 文件,我正在尝试使用 sqlcmd 在我的 C# 代码中自动 运行。我使用 ProcessStartInfo 启动命令提示符,然后从那里 运行 sqlcmd。但是,一旦 sqlcmd 完成 运行 宁第一个文件,命令 window 保持运行并且我的应用程序挂在 "Wait for Exit"。如何使我的命令提示符在执行我的 sql 文件后退出?

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
    }
}

从命令提示符:

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

尝试在下一行中用 /C 替换 /K

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