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.

3
  • 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 stackoverflow.com/questions/49025374/… Commented Nov 29, 2020 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()) stackoverflow.com/questions/5795430/… Commented Nov 29, 2020 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. Commented Nov 29, 2020 at 21:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.