简体   繁体   English

在C#中从存储过程返回多个值

[英]Return multiple values from Stored Procedure in C#

I need to return 2 values from stored procedure in my application. 我需要从应用程序中的存储过程中返回2个值。 Below is the code snippet in my application. 下面是我的应用程序中的代码片段。 I need to get the values of SureveyID & InputID below after the respective insert statements. 在相应的插入语句之后,我需要在下面获取SureveyID和InputID的值。

        int surveyId = 0;
        int inputId = 0;    
        SqlDataManager manager = new SqlDataManager();
        manager.AddParameter("@Name", surveyInstance.SurveyName);
        manager.AddParameter("@Type", surveyInstance.SurveyType);
        manager.AddParameter("@UserId", surveyInstance.UserId);
        manager.AddParameter("@InputType", surveyInstance.InputType);
        manager.AddParameter("@DisplayName", surveyInstance.DisplayName);
        manager.AddOutputParameter("@SurveyID",System.Data.DbType.Int32,surveyId);
        manager.AddOutputParameter("@InputID", System.Data.DbType.Int32,inputId);
        manager.ExecuteNonQuery("pr_CreateSurvey");

AddParameter & AddOutputParameter is custom method as below AddParameter和AddOutputParameter是如下的自定义方法

 public void AddParameter(string parameterName, DbType parameterType, object        parameterValue)
    {
        SqlParameter parameter = new SqlParameter();
        parameter.ParameterName = parameterName;
        parameter.DbType = parameterType;
        parameter.Value = parameterValue;
        parameters.Add(parameter);
    }

public void AddOutputParameter(string parameterName, DbType parameterType, object parameterValue)
        {
            SqlParameter parameter = new SqlParameter();
            parameter.ParameterName = parameterName;
            parameter.DbType = parameterType;
            parameter.Value = parameterValue;
            parameter.Direction = ParameterDirection.Output;
            parameters.Add(parameter);
        }

Below is code snippet from stored procedure 下面是存储过程中的代码片段

ALTER PROCEDURE [dbo].[pr_CreateSurvey] 
    -- Add the parameters for the stored procedure here
    @Name varchar(50),@Type varchar(50),@UserId varchar(50),@InputType varchar(50),@DisplayName varchar(50),
    @SurveyID int OUTPUT,@InputID int OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    Insert into surveys(name,user_id,display_name,type) values(@Name,@UserId,@DisplayName,@Type)
    SET @SurveyID = SCOPE_IDENTITY()
    Insert into input_types(name) values (@InputType)
    SET @InputID = SCOPE_IDENTITY()
END

The insert statements are working fine but I am not getting back any value in my application. insert语句运行良好,但我的应用程序未获得任何价值。 Its 0. I tried returning 1 value(SurveyID) by using below statement but still not getting correct value. 它的值为0。我尝试通过使用以下语句返回1值(SurveyID),但仍未获得正确的值。 Its returning -1 everytime. 每次返回-1。

surveyId = manager.ExecuteNonQuery("pr_CreateSurvey");

I tried a lot but no luck. 我尝试了很多但是没有运气。 Please advise. 请指教。

find the values of the output parameters in the Parameters collection of your SqlCommand ... like 在SqlCommand的Parameters集合中找到输出参数的值...

mySqlCommand.Parameters["@SurveyID"].Value

after you executed 执行后

mySqlCommand.ExecuteNonQuery();

Keep a variable of your ouput parameters around and check the .Value afterwards. 保留输出参数的变量,然后检查.Value。 The ouput gets written to the parameter, but due to boxing, it does not get written to your int. 输出被写入参数,但是由于装箱,它不会被写入您的int。 Basically, your int was copied into the parameter and your original variables do not change. 基本上,您的int已复制到参数中,并且原始变量不会更改。

ExecuteNonQuery returns the number of rows affected, not anything else. ExecuteNonQuery返回受影响的行数,而不是其他任何行。

I've never used output parameters in my stored procedures for this. 我从未在存储过程中使用过输出参数。 What I usually do is select out the values I want to return. 我通常要做的是选择要返回的值。

So after you do this: 因此,执行此操作后:

SET @SurveyID = SCOPE_IDENTITY()
SET @InputID = SCOPE_IDENTITY()

I will do this 我会做的

select @SurveyID as SurveyID,@InputID as InputID

Of course I'll declare those variables within the stored procedure and not as OUTPUT 当然,我将在存储过程中声明这些变量,而不是将其声明为OUTPUT

This should give you what you want. 这应该给您您想要的。 If it's the correct way? 如果是正确的方法? Not sure. 不确定。 But it sure as hell works and is easy ;) 但是可以肯定的是,它确实可行并且很容易;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从C#中的存储过程获取返回值(登录过程) - Get return values from a stored procedure in c# (login process) 如何从存储过程返回表中的多行并使用 ADO.NET 在 C# 中访问这些值? - How to return table multiple rows from a stored procedure and access those values in C# using ADO.NET? 如何将存储过程中的值从SQL返回到C# - How do I return values from my stored procedure from SQL to c# 从存储过程返回数据的对象返回列表C# - Return List of Objects from stored procedure return data C# 如何在数据库上下文中运行没有C#返回值的存储过程? - How can I run a stored procedure that has no return values from C# with my db context? 尝试从必须返回值的C#形式的存储过程中查找值 - Trying to find values from a stored procedure in c# form that must return value C# & ASP.NET MVC 5 - 从按钮单击执行存储过程,没有返回值 - C# & ASP.NET MVC 5 - execute stored procedure from button click with no return values 如何从C#函数的存储过程中返回多个输出参数 - How to return multiple output parameters from stored procedure for C# function 从C#中的存储过程返回值 - Return values from stored procedures in C# 从C#中的存储过程返回最后一个SELECT值 - Return last SELECT value from stored procedure in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM