如何获得存储过程的返回值 [英] How to get Return Value of a Stored Procedure

查看:133
本文介绍了如何获得存储过程的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能会是一个容易回答的问题。我有这样的过程:

  CREATE PROCEDURE [DBO]。[AccountExists]
    @UserName为nvarchar(16)
如
IF EXISTS(SELECT ID从客户其中username = @用户名)
选择1
否则选择0
 

当我有ADO.NET code调用该程序并执行这样的:

 返回Convert.ToBoolean(sproc.ExecuteScalar());
 

true或false返回。

当我更改存储过程返回1或​​0,而不是选择:

  ALTER PROCEDURE [DBO]。[AccountExists]
    @UserName为nvarchar(16)
如
IF EXISTS(SELECT ID从客户其中username = @用户名)
返回1
否则返回0
 

sproc.ExecuteScalar()返回null。如果我尝试sproc.ExecuteNonQuery()来代替,则返回-1。

我如何在ADO.NET一回存储过程的结果?

我需要AccountExists至RETURN,而不是选择的,所以我可以有另外一个存储过程调用它:

   - 另一个程序插入或更新帐户

DECLARE @exists位

EXEC @exists = [DBO]。[AccountExists] @UserName

@如果存在= 1
--update账户
其他
 --insert acocunt
 

解决方案

ParameterDirection.ReturnValue 添加参数的命令,使用。返回值将在present在执行后,参数中。

Probably an easy-to-answer question. I have this procedure:

CREATE PROCEDURE [dbo].[AccountExists]
    @UserName nvarchar(16)
AS
IF EXISTS (SELECT Id FROM Account WHERE UserName=@UserName)
SELECT 1
ELSE SELECT 0

When I have ADO.NET code that calls this procedure and does this:

return Convert.ToBoolean(sproc.ExecuteScalar());

Either true or false is returned.

When I change the stored procedure to RETURN 1 or 0 instead of SELECT:

ALTER PROCEDURE [dbo].[AccountExists]
    @UserName nvarchar(16)
AS
IF EXISTS (SELECT Id FROM Account WHERE UserName=@UserName)
RETURN 1
ELSE RETURN 0

sproc.ExecuteScalar() returns null. If I try sproc.ExecuteNonQuery() instead, -1 is returned.

How do I get the result of a stored procedure with a RETURN in ADO.NET?

I need AccountExists to RETURN instead of SELECT so I can have another stored procedure call it:

--another procedure to insert or update account

DECLARE @exists bit

EXEC @exists = [dbo].[AccountExists] @UserName 

IF @exists=1
--update account
ELSE
 --insert acocunt

解决方案

Add a parameter to the command, using ParameterDirection.ReturnValue. The return value will be present in the paramter after the execution.

这篇关于如何获得存储过程的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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