Skip to content

Instantly share code, notes, and snippets.

@binki
Last active May 24, 2017 16:24
Show Gist options
  • Save binki/6fba7e433389364ce778f03b34b513ae to your computer and use it in GitHub Desktop.
Save binki/6fba7e433389364ce778f03b34b513ae to your computer and use it in GitHub Desktop.
SqlCommand does read OUTPUT from stored procedure even when RAISERROR
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