我有一个存储过程,它执行一些操作并返回 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 0ExcuteScalar
将捕获它.. 但是否有任何方法可以让我通过将 return
替换为 select
来获取返回值?
注意:我也不想用户存储过程输出参数
最佳答案
本质上,您必须定义一个带有“方向”返回值的参数。
您可以在这里找到一些示例:Using ADO.NET with SQL Server在这里:Get the Return Value from an SQL Stored Procedure .
这是引用的第一个链接的相关部分:
To get the value of a RETURN statement, you need to do the following:
// add a new parameter, with any name we want - its for our own
// use only
SqlParameter sqlParam = com.Parameters.Add("@ReturnValue", SqlDbType.Int);
// set the direction flag so that it will be filled with the return value
myParm.Direction = ParameterDirection.ReturnValue;
Then, after the stored procedure has been executed,
int returnValue = (int)com.Parameters["@ReturnValue"].Value
will retrieve the value that was set.
关于c# - 如何在 C# 中获取存储过程的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3123187/