如何从存储过程中获取返回值?

时间:2021-04-15 08:51:32

Here's my SQL:

这是我的SQL:

IF (SELECT Status FROM dbo.Coupon WHERE Guid = @pGuid) = 0
BEGIN
    UPDATE
        dbo.Coupon

    SET
        Status = @pStatus   

    WHERE
        Guid = @pGuid

    RETURN 0    
END

ELSE

RETURN 1;

And here's my C#:

这是我的C#:

try
            {
                DbCommand command = db.GetStoredProcCommand("upd_Coupon_p");
                db.AddInParameter(command, "@pGuid", DbType.String, s);
                db.AddInParameter(command, "@pStatus", DbType.Byte, 1);
                ds = db.ExecuteDataSet(command);
             }

How can I get the return value of 0 or 1 inside of my code?

如何在代码中获得0或1的返回值?

4 个解决方案

#1


5  

You add a return value parameter, like this:

您添加一个返回值参数,如下所示:

For SqlCommand:

对于SqlCommand:

parameters.Add("@retValue", DbType.Int32, ParameterDirection.ReturnValue);   

For the EL, you'd want to use db.AddParameter() and specify ParameterDirection.ReturnValue.

对于EL,您需要使用db.AddParameter()并指定ParameterDirection.ReturnValue。

In addition, as long as row count is on in your database, for the update you are performing you could use the result from ExecuteNonQuery() that tells you how many rows were affected on an update/insert/delete/etc. That way you could handle if rows affected was 0 (couldn't find any)

此外,只要您的数据库中启用了行计数,对于您正在执行的更新,您可以使用ExecuteNonQuery()中的结果来告诉您在更新/插入/删除/等上受影响的行数。这样你可以处理受影响的行是0(找不到任何)

#2


0  

This is what I did, so basically just use ReturnValue, but the other parts may be useful.

这就是我所做的,所以基本上只使用ReturnValue,但其他部分可能很有用。

            var retparam = new SqlParameter("@return", System.Data.SqlDbType.Int) { Direction = System.Data.ParameterDirection.ReturnValue };
            comm.Parameters.Add(retparam);
            comm.ExecuteNonQuery();
            int ret = 0;
            if (retparam == null)
            {
                System.Diagnostics.Debug.WriteLine("retparam was null");
            }
            else if (retparam.Value == null)
            {
            }
            else
            {
               // use reparam.Value.ToString()
            }

#3


-1  

What is DbCommand.ExecuteDataSet() and why don't you use ExecuteScalar()?

什么是DbCommand.ExecuteDataSet()以及为什么不使用ExecuteScalar()?

#4


-1  

Declare a variable as output and get it inside the call function of Data Access section.

声明一个变量作为输出,并将其置于数据访问部分的调用函数中。

see the code below,

看下面的代码,

In the stored procedure,

在存储过程中,

  @ReturnStatus int output //inside your stored procedure argument section

In the Data Access section use the following,

在“数据访问”部分中,使用以下内容,

   AddOutParameter(.....);

Hope this helps..

希望这可以帮助..

#1


5  

You add a return value parameter, like this:

您添加一个返回值参数,如下所示:

For SqlCommand:

对于SqlCommand:

parameters.Add("@retValue", DbType.Int32, ParameterDirection.ReturnValue);   

For the EL, you'd want to use db.AddParameter() and specify ParameterDirection.ReturnValue.

对于EL,您需要使用db.AddParameter()并指定ParameterDirection.ReturnValue。

In addition, as long as row count is on in your database, for the update you are performing you could use the result from ExecuteNonQuery() that tells you how many rows were affected on an update/insert/delete/etc. That way you could handle if rows affected was 0 (couldn't find any)

此外,只要您的数据库中启用了行计数,对于您正在执行的更新,您可以使用ExecuteNonQuery()中的结果来告诉您在更新/插入/删除/等上受影响的行数。这样你可以处理受影响的行是0(找不到任何)

#2


0  

This is what I did, so basically just use ReturnValue, but the other parts may be useful.

这就是我所做的,所以基本上只使用ReturnValue,但其他部分可能很有用。

            var retparam = new SqlParameter("@return", System.Data.SqlDbType.Int) { Direction = System.Data.ParameterDirection.ReturnValue };
            comm.Parameters.Add(retparam);
            comm.ExecuteNonQuery();
            int ret = 0;
            if (retparam == null)
            {
                System.Diagnostics.Debug.WriteLine("retparam was null");
            }
            else if (retparam.Value == null)
            {
            }
            else
            {
               // use reparam.Value.ToString()
            }

#3


-1  

What is DbCommand.ExecuteDataSet() and why don't you use ExecuteScalar()?

什么是DbCommand.ExecuteDataSet()以及为什么不使用ExecuteScalar()?

#4


-1  

Declare a variable as output and get it inside the call function of Data Access section.

声明一个变量作为输出,并将其置于数据访问部分的调用函数中。

see the code below,

看下面的代码,

In the stored procedure,

在存储过程中,

  @ReturnStatus int output //inside your stored procedure argument section

In the Data Access section use the following,

在“数据访问”部分中,使用以下内容,

   AddOutParameter(.....);

Hope this helps..

希望这可以帮助..