Written for this SO question about TRY…CATCH
in T-SQL.
Last active
May 24, 2017 16:24
-
-
Save binki/6fba7e433389364ce778f03b34b513ae to your computer and use it in GitHub Desktop.
SqlCommand does read OUTPUT from stored procedure even when RAISERROR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data; | |
using System.Data.SqlClient; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (SqlConnection connection = new SqlConnection(new SqlConnectionStringBuilder | |
{ | |
DataSource = "localhost\\SQLEXPRESS13", | |
InitialCatalog = "dcx6test", | |
IntegratedSecurity = true, | |
}.ConnectionString)) | |
using (var command = new SqlCommand("dbo.p_x", connection) | |
{ | |
CommandType = CommandType.StoredProcedure, | |
}) | |
{ | |
connection.Open(); | |
var OUT_x = new SqlParameter("OUT_x", 0) | |
{ | |
Direction = ParameterDirection.Output, | |
}; | |
command.Parameters.Add(OUT_x); | |
try | |
{ | |
command.ExecuteNonQuery(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); // yo | |
} | |
Console.WriteLine($"OUT_x={OUT_x.Value}"); // OUT_x=1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment