如何运行具有OUTPUT参数从C#存储过程? [英] How to run the stored procedure that has OUTPUT parameter from C#?

查看:343
本文介绍了如何运行具有OUTPUT参数从C#存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输出参数的存储过程。我如何使用C#code读取该值?

I have a stored procedure with an output parameter. How do I read this value using C# code?

推荐答案

我假设你使用ADO.NET?如果是这样,的SqlParameter类具有属性方向。设定方向和输出后,查询执行你读从参数的值。

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.

事情是这样的:

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();
}

这篇关于如何运行具有OUTPUT参数从C#存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆