如何在 C# 中获取存储过程的返回值?

发布于 2024-09-07 15:18:21 字数 501 浏览 1 评论 0原文

我有一个存储过程,它执行一些操作并返回 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 0Select 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哎呦我呸! 2024-09-14 15:18:21

本质上,您必须定义一个具有“方向”返回值的参数。

您可以在此处找到一些示例: 使用 ADO.NET使用 SQL Server 和此处:从 SQL 存储过程获取返回值

这是引用的第一个链接中的相关部分:

获取 RETURN 的值
声明,您需要执行以下操作
以下:

// 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;

然后,在存储过程完成后
已被处决,

int returnValue = (int)com.Parameters["@ReturnValue"].Value

将检索设置的值。

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:

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.

〃温暖了心ぐ 2024-09-14 15:18:21

添加参数时,有一个“返回值”参数方向:

var param = cmd.Parameters.Add(cmd.CreateParameter());
param.Direction = System.Data.ParameterDirection.ReturnValue;

调用(ExecuteNonQuery)后检查该参数的.Value,即可得到返回值。不过,有一点要注意:当将其与SELECT结果一起使用时,返回值仅在从结果中读取所有数据后才可用(返回/输出位于末尾)。

When adding parameters, there is a "return value" parameter direction:

var param = cmd.Parameters.Add(cmd.CreateParameter());
param.Direction = System.Data.ParameterDirection.ReturnValue;

Inspect the .Value of this param after the call (ExecuteNonQuery), and you have the return value. One note, though; when using this with SELECT 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).

沉睡月亮 2024-09-14 15:18:21

您需要添加一个参数,其方向 = ReturnValue:

using (var command = new SqlCommand("dbo.CheckUsername11", conn) 
            { CommandType = CommandType.StoredProcedure })
{
  command.Parameters.Add(new SqlParameter("@result") { ParameterDirection.ReturnValue });
  command.Parameters.AddWithValue("@Username", username);

  command.ExecuteNonQuery();

  return (int)command.Parameters["@result"].Value;
}

You need to add a parameter with a direction = ReturnValue:

using (var command = new SqlCommand("dbo.CheckUsername11", conn) 
            { CommandType = CommandType.StoredProcedure })
{
  command.Parameters.Add(new SqlParameter("@result") { ParameterDirection.ReturnValue });
  command.Parameters.AddWithValue("@Username", username);

  command.ExecuteNonQuery();

  return (int)command.Parameters["@result"].Value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文