开发者

How to run the stored procedure that has OUTPUT parameter from C#?

I have a stored procedure with an output p开发者_开发技巧arameter. How do I read this value using C# code?


I assume you use ADO.NET? If so, the SqlParameter class has the property "Direction". Set direction to output and after the query has executed you read the value from that parameter.

Something like this:

using (SqlCommand cmd = new SqlCommand("MyStoredProcedure", cn))
{
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter parm = new SqlParameter("@pkid", SqlDbType.Int);
    parm.Value = 1;
    parm.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(parm);

    SqlParameter parm2 = new SqlParameter("@ProductName", SqlDbType.VarChar);
    parm2.Size = 50;
    parm2.Direction = ParameterDirection.Output; // This is important!
    cmd.Parameters.Add(parm2);

    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜