I have this stored procedure
CREATE PROCEDURE [dbo].[TestProcedure]
@param1 int = 0
,@param2 int = 0
,@total_sales int = 5 OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET @total_sales = @total_sales * 5
SELECT * FROM SomeTable
END
And this string in C#
string strSQL = @"
DECLARE @RC int
DECLARE @param1 int
DECLARE @param2 int
DECLARE @total_sales int
-- TODO: Set parameter values here.
SET @param1 = 1
SET @param2 = 2
EXECUTE @RC = [TestDB].[dbo].[TestProcedure]
@param1
,@param2
,@total_sales OUTPUT";
And now I want to retrieve the output value, but without parametrizing the input query !
I tried this:
using (System.Data.SqlClient.SqlCommand cmd = (System.Data.SqlClient.SqlCommand)idbConn.CreateCommand())
{
cmd.CommandText = strSQL;
cmd.Transaction = (System.Data.SqlClient.SqlTransaction)idbtTrans;
iAffected = cmd.ExecuteNonQuery();
idbtTrans.Commit();
string strOutputParameter = cmd.Parameters["@total_sales"].Value.ToString();
Console.WriteLine(strOutputParameter);
} // End Using IDbCommand
And this throws an exception (the parameter @total_sales is not in the parameter list).
How can I retrieve an output parameter in a non-parametrized stored-procedure call WITHOUT parametrizing the query ?