<p>I need to run a stored procedure from a C# application.</p>

<p>I use the following code to do so:</p>

<pre><code>Process sqlcmdCall = new Process();
sqlcmdCall.StartInfo.FileName = "sqlcmd.exe";
sqlcmdCall.StartInfo.Arguments = "-S localhost\\SQLEXPRESS -d some_db -Q \":EXIT(sp_test)\""
sqlcmdCall.Start();
sqlcmdCall.WaitForExit();
</code></pre>

<p>From the sqlcmdCall object after the call completes, I currently get an ExitCode of -100 for success and of 1 for failure (i.e. missing parameter, stored proc does not exist, etc...).</p>

<p>How can I customize these return codes?</p>

<p>H.</p>

[{'answer_id': 73108, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': True, 'text': '<p>If you are trying to call a stored procedure from c# you would want to use ADO.Net instead of the calling sqlcmd via the command line.  Look at <code>SqlConnection</code> and <code>SqlCommand</code> in the <code>System.Data.SqlClient</code> namespace.</p>\n\n<p>Once you are calling the stored procedure via <code>SqlCommand</code> you will be able to catch an exception raised by the stored procedure as well we reading the return value of the procedure if you need to.</p>\n'}, {'answer_id': 73193, 'author': 'Ron Savage', 'author_id': 12476, 'author_profile': 'https://Stackoverflow.com/users/12476', 'pm_score': 2, 'selected': False, 'text': '<p>I have a small VB.Net app that executes system commands like that. To capture error or success conditions I define regular expressions to match the error text output from the command and I capture the output like this:</p>\n\n<pre><code>        myprocess.Start()\n        procReader = myprocess.StandardOutput()\n\n        While (Not procReader.EndOfStream)\n           procLine = procReader.ReadLine()\n\n           If (MatchesRegEx(errRegEx, procLine)) Then\n              writeDebug("Error reg ex: [" + errorRegEx + "] has matched: [" + procLine + "] setting hasError to true.")\n\n              Me.hasError = True\n           End If\n\n           writeLog(procLine)\n        End While\n\n        procReader.Close()\n\n        myprocess.WaitForExit(CInt(waitTime))\n</code></pre>\n\n<p>That way I can capture specific errors and also log all the output from the command in case I run across an unexpected error.</p>\n'}, {'answer_id': 77400, 'author': 'Mitchel Sellers', 'author_id': 13279, 'author_profile': 'https://Stackoverflow.com/users/13279', 'pm_score': 0, 'selected': False, 'text': "<p>Even with windows authentication you can still use <code>SqlCommand</code> and <code>SqlConnection</code> to execute, and you don't have to re-invent the wheel for exception handling.</p>\n\n<p>A simple connection configuration and a single <code>SqlCommand</code> can execute it without issue.</p>\n"}]
['https://Stackoverflow.com/questions/73051', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12525/']