如何在 C# 中获取存储过程的返回值?
我有一个存储过程,它执行一些操作并返回 1 或 0,如下所示
CREATE PROCEDURE dbo.UserCheckUsername11
(
@Username nvarchar(50)
)
AS
BEGIN
SET NOCOUNT ON;
IF Exists(SELECT UserID FROM User WHERE username =@Username)
return 1
ELSE
return 0
END
,在 C# 中,我想获取此返回值,我尝试 ExcuteScalar
但它没有返回任何值(我知道我是否替换return 0
和 Select 0
ExcuteScalar
将捕获它..但是是否有任何方法可以让我通过替换 return 获得返回值
和 select
注意:我也不想使用存储过程输出参数
I have a stored Procedure that do some operation and return 1 or 0 as below
CREATE PROCEDURE dbo.UserCheckUsername11
(
@Username nvarchar(50)
)
AS
BEGIN
SET NOCOUNT ON;
IF Exists(SELECT UserID FROM User WHERE username =@Username)
return 1
ELSE
return 0
END
and in C# i want to get this returned value i try ExcuteScalar
but it didn't return any value ( i know if i replace return 0
with Select 0
ExcuteScalar
will catach it .. but is it any way that allow me to get returned value with replace return
with select
?
NOTE : I don't want to user stored procedure output parameter also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
本质上,您必须定义一个具有“方向”返回值的参数。
您可以在此处找到一些示例: 使用 ADO.NET使用 SQL Server 和此处:从 SQL 存储过程获取返回值。
这是引用的第一个链接中的相关部分:
In essence you have to define a parameter with "direction" return value.
You can find some examples here: Using ADO.NET with SQL Server and here: Get the Return Value from an SQL Stored Procedure.
Here comes the relevant part from the first link cited:
添加参数时,有一个“返回值”参数方向:
调用(
ExecuteNonQuery
)后检查该参数的.Value
,即可得到返回值。不过,有一点要注意:当将其与SELECT
结果一起使用时,返回值仅在从结果中读取所有数据后才可用(返回/输出位于末尾)。When adding parameters, there is a "return value" parameter direction:
Inspect the
.Value
of this param after the call (ExecuteNonQuery
), and you have the return value. One note, though; when using this withSELECT
results, the return value is only available after all the data has been read from the results (return/output is at the end of the TDS stream).您需要添加一个参数,其方向 = ReturnValue:
You need to add a parameter with a direction = ReturnValue:
检查以下网址是否存在相同问题
1- 如何获取存储的返回值流程
2- 获取C# asp.net 中存储过程的返回值(语法问题)
Check the following URLs it contain the same problem
1- How to get Return Value of a Stored Procedure
2- Getting a Return Value in C# asp.net from a stored procedure (syntax issue)