我还没有看到任何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)