Інший відповідь показує , що це, але по суті вам просто потрібно створити SqlParameter, встановити , Directionщоб Output, і додати його в SqlCommand«s Parametersколекції. Потім виконайте збережену процедуру та отримайте значення параметра.
Використовуючи зразок коду:
// SqlConnection and SqlCommand are IDisposable, so stack a couple using()'s
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("sproc", conn))
{
// Create parameter with Direction as Output (and correct name and type)
SqlParameter outputIdParam = new SqlParameter("@ID", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(outputIdParam);
conn.Open();
cmd.ExecuteNonQuery();
// Some various ways to grab the output depending on how you would like to
// handle a null value returned from the query (shown in comment for each).
// Note: You can use either the SqlParameter variable declared
// above or access it through the Parameters collection by name:
// outputIdParam.Value == cmd.Parameters["@ID"].Value
// Throws FormatException
int idFromString = int.Parse(outputIdParam.Value.ToString());
// Throws InvalidCastException
int idFromCast = (int)outputIdParam.Value;
// idAsNullableInt remains null
int? idAsNullableInt = outputIdParam.Value as int?;
// idOrDefaultValue is 0 (or any other value specified to the ?? operator)
int idOrDefaultValue = outputIdParam.Value as int? ?? default(int);
conn.Close();
}
Будьте обережні при отриманні Parameters[].Value, оскільки тип повинен бути відтворений з objectтого, що ви заявляєте. І SqlDbTypeвикористовується при створенні SqlParameterпотреб для відповідності типу в базі даних. Якщо ви збираєтеся просто вивести його на консоль, можливо, ви просто використовуєте Parameters["@Param"].Value.ToString()(або явно, або неявно через Console.Write()або String.Format()виклик).
РЕДАГУВАТИ: Більше 3,5 років та майже 20 тис. Переглядів, і ніхто не потрудився згадати, що він навіть не склався з причини, зазначеної в моєму коментарі "будь обережним" в оригінальному дописі. Приємно. Виправлено на основі хороших коментарів від @Walter Stabosz та @Stephen Kennedy та відповідно до редагування коду оновлення у питанні від @abatishchev.
conn.Close()як це всерединіusingблоку