开发者

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

开发者 https://www.devze.com 2023-01-11 03:14 出处:网络
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 di

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

精彩评论

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