2

我有这个程序:

ALTER PROCEDURE [xyz].[stored_proc]
    @input1 as int, 
    @input2 as nvarchar(255)
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE ...
    SET input1=@input1
    WHERE input2=@input2 AND
    input3 NOT IN (123)
END

我想捕获更新子句输出并将其作为过程输出参数返回,这样我就可以在我的 .net 代码中处理它。我怎样才能做到这一点?

4

1 回答 1

2

我还没有看到任何OUTPUT子句 - 但如果你确实有它,你需要做的只是运行这个存储过程,就好像它返回了一个结果集(来自SELECT语句):

using (SqlConnection conn = new SqlConnection(-your-connection-string-here-))
using (SqlCommand cmd = new SqlCommand("[xyz].[stored_proc]", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;

    conn.Open();

    // execute your UPDATE statement with an OUTPUT clause into a SqlDataReader
    using(SqlDataReader reader = cmd.ExecuteReader())
    {
        // read the values returned from the OUTPUT clause
        while(reader.Read())
        {
            int insertedID = reader.GetInt32(0);
            // do something with those values....                
        }
    }

    conn.Close();
}

这是假设您的存储过程中的 UPDATE 语句看起来像这样:

UPDATE ...
OUTPUT Inserted.ID    -- or something else....
SET input1 = @input1
WHERE input2 = @input2 
  AND input3 NOT IN (123)
于 2013-05-20T08:03:31.330 回答