SQL Server输出参数 [英] SQL Server Output Parameter

查看:70
本文介绍了SQL Server输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从存储过程中查看Output参数的值.代码如下

I am not able to view value of a Output parameter from my stored procedure. The code is as below

SqlParameter paruniqueid = new SqlParameter("@uniqueid", uniqueid);
paruniqueid.Direction = ParameterDirection.Output;
cmd.Parameters.Add("@uniqueid", uniqueid);

try
            {
                if(cmd.ExecuteNonQuery() >= 1)
                {
                    int newValue = Convert.ToInt32(paruniqueid.Value);
                    cn.Close();
                    return newValue;
                    //return str;
                }
                else
                {
                    cn.Close();
                    return 0;
                    //return str;
                }
            }



并在存储过程中将其声明为



and in the stored procedure it is declared as

@uniqueid int output 

set @uniqueid = (Select max(UserAllergyId) from UserAllergy)



该值将作为0



The value is coming as 0

推荐答案

尝试以下操作:

Try this one:

SqlParameter pUniqueID = new SqlParameter("@uniqueid", SqlDbType.Int32);
cmd.Parameters.Add(pUniqueID);
pUniqueID .Direction = ParameterDirection.Output;

cmd.ExecuteNonQuery();

if (!(Int32.IsNullOrEmpty(Int32.Parse(cmd.Parameters["@uniqueid"].Value))))
   int retval = (int)cmd.Parameters["@uniqueid"].Value;



-爱德华



-Eduard


这篇关于SQL Server输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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