title: How to terminate sqlcmd immediately after execution completed?
body: <p>I create a process in C# to execute <code>sqlcmd /S &lt;servername&gt; /d &lt;dbname&gt; /E /i</code> to run sql script that create tables, views, stored procedures.</p>
  
  <p>The problem is the process does not terminate after the execution is completed. I want to know how can I terminate sqlcmd immediately after the operation is completed.</p>
  
  <p>Is there any input argument to sqlcmd that I can specify to terminate the process right away or can I do it directly in C#?</p>
  
  <p><strong>Update</strong></p>
  
  <p>This is the C# code that I use to execute the process.</p>
  
  <pre><code>foreach (var file in files)
  {
      ////var begin = DateTime.Now;
      ////context.TrackBuildWarning(string.Format("Start exec sql file at {0}.", DateTime.Now));
  
      Process process = new Process();
      process.StartInfo.UseShellExecute = false;
      process.StartInfo.RedirectStandardOutput = true;
      process.StartInfo.RedirectStandardError = true;
      process.StartInfo.CreateNoWindow = true;
      process.StartInfo.FileName = "sqlcmd.exe";
      process.StartInfo.Arguments = string.Format("-S {0} -d {1} -i \"{2}\" -U {3} -P {4}", sqlServerName, databaseName, file, sqlUserName, sqlPassword);
      process.StartInfo.WorkingDirectory = @"C:\";
      process.Start();
      process.WaitForExit(); // if I comment out this code it will execute much faster
  
      ////context.TrackBuildWarning(string.Format("Finished exec sql file at {0} total time {1} milliseconds.", DateTime.Now, DateTime.Now.Subtract(begin).TotalMilliseconds));
  }
  </code></pre>
  
  <p>As you can see, I have comment that if I remove (or comment) out <code>process.WaitForExit()</code> it will execute A LOT faster.</p>