Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. // Start SQLCMD process.
  2. ProcessStartInfo psi = new ProcessStartInfo();
  3. psi.FileName = "SQLCMD.EXE";
  4. string args = "-S " + this.Server;
  5. if (string.IsNullOrEmpty(this.UserName)) args += " -E";
  6. else args += " -U " + this.UserName;
  7. if (!string.IsNullOrEmpty(scriptFile.Database))
  8. args += " -d " + scriptFile.Database;
  9. if (string.IsNullOrEmpty(this.Password))
  10. args += " -i " + scriptFilePath;
  11. else
  12. args += " -P " + this.Password + " -i " + scriptFilePath;
  13. psi.Arguments = args;
  14. psi.RedirectStandardOutput = true;
  15. psi.RedirectStandardError = true;
  16. psi.UseShellExecute = false;
  17.  
  18. using (Process executorProcess = Process.Start(psi))
  19. {
  20. executorProcess.EnableRaisingEvents = true;
  21. executorProcess.OutputDataReceived +=
  22. new DataReceivedEventHandler(
  23. this.executorProcess_DataReceived);
  24. executorProcess.ErrorDataReceived +=
  25. new DataReceivedEventHandler(executorProcess_ErrorDataReceived);
  26.  
  27. executorProcess.BeginOutputReadLine();
  28. executorProcess.BeginErrorReadLine();
  29. bool exited = false;
  30. while (!exited)
  31. {
  32. exited = executorProcess.WaitForExit(60000);
  33. if (!exited && this.StopSignal)
  34. {
  35. try { executorProcess.Kill(); }
  36. catch { }
  37. }
  38. }
  39. try
  40. {
  41. executorProcess.CancelOutputRead();
  42. executorProcess.CancelErrorRead();
  43. }
  44. catch { }
  45.  
  46. void executorProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  47. {
  48. Console.WriteLine(e.Data);
  49. }
  50.  
  51. private void executorProcess_DataReceived(object sender,
  52. DataReceivedEventArgs e)
  53. {
  54. Console.WriteLine(e.Data);
  55. }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement