0

I use sqlcmd to run large script SQL from C# code:

ProcessStartInfo info = new ProcessStartInfo("sqlcmd", @" -S . -i "+ currentDBScript.Value + " -b");
 
info.UseShellExecute = false;
//No new window is required
info.CreateNoWindow = true;
//The windows style will be hidden
info.WindowStyle = ProcessWindowStyle.Hidden;
//The output will be read by the starndar output process
info.RedirectStandardOutput = true;

Process proc = new Process();
proc.StartInfo = info;

//Start the process
proc.Start();

Note that currentDBScript.Value is the path of my file script

I want to catch error if script executed by sqlcmd didn't succeed.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I don't think that sqlcmd has been built with the capability to talk directly with extern application, but you can ask it to write a log file that you can parse for errors. An example here https://stackoverflow.com/questions/49025374/sqlcmd-command-how-to-save-output-into-log-file – Steve Nov 29 '20 at 18:10
  • otherwise you create a batch file which interacts with sqlcmd commands where you can capture the errors and throw exceptions, (info.StandardError.ReadToEnd()) https://stackoverflow.com/questions/5795430/error-handling-with-batch-file-sqlcmd – coder_b Nov 29 '20 at 19:56
  • I've used same code lots of times and it should work. You need the full path of the SQLCMD.EXE unless it is in same folder and the c# executable. – jdweng Nov 29 '20 at 21:18

0 Answers0