Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Start SQLCMD process.
- ProcessStartInfo psi = new ProcessStartInfo();
- psi.FileName = "SQLCMD.EXE";
- string args = "-S " + this.Server;
- if (string.IsNullOrEmpty(this.UserName)) args += " -E";
- else args += " -U " + this.UserName;
- if (!string.IsNullOrEmpty(scriptFile.Database))
- args += " -d " + scriptFile.Database;
- if (string.IsNullOrEmpty(this.Password))
- args += " -i " + scriptFilePath;
- else
- args += " -P " + this.Password + " -i " + scriptFilePath;
- psi.Arguments = args;
- psi.RedirectStandardOutput = true;
- psi.RedirectStandardError = true;
- psi.UseShellExecute = false;
- using (Process executorProcess = Process.Start(psi))
- {
- executorProcess.EnableRaisingEvents = true;
- executorProcess.OutputDataReceived +=
- new DataReceivedEventHandler(
- this.executorProcess_DataReceived);
- executorProcess.ErrorDataReceived +=
- new DataReceivedEventHandler(executorProcess_ErrorDataReceived);
- executorProcess.BeginOutputReadLine();
- executorProcess.BeginErrorReadLine();
- bool exited = false;
- while (!exited)
- {
- exited = executorProcess.WaitForExit(60000);
- if (!exited && this.StopSignal)
- {
- try { executorProcess.Kill(); }
- catch { }
- }
- }
- try
- {
- executorProcess.CancelOutputRead();
- executorProcess.CancelErrorRead();
- }
- catch { }
- void executorProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
- {
- Console.WriteLine(e.Data);
- }
- private void executorProcess_DataReceived(object sender,
- DataReceivedEventArgs e)
- {
- Console.WriteLine(e.Data);
- }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement