我有这个存储过程
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
而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";
现在我想检索输出值,但不对输入查询进行参数化!
我试过这个:
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
这会引发异常(参数@total_sales 不在参数列表中)。
如何在不参数化查询的情况下在非参数化存储过程调用中检索输出参数?