如何获得存储过程的返回值

编程入门 行业动态 更新时间:2024-10-19 00:29:12
本文介绍了如何获得存储过程的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

如何获得存储过程的返回值

本文发布于:2023-11-06 13:55:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存储过程   如何获得   返回值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!