C#-接受参数并返回值的SQL过程

问题描述 投票:0回答:1

所以,我正在为一个类编写应用程序,我已经在使用存储过程来进行插入,删除,更改,查看。但是这个,这个接受一个参数,然后又给出另一个值。如何在我的代码中实现呢?这是SQL:

USE [ADK_DB]
GO
/****** Object:  StoredProcedure [IO].[usp_ukupno_donacija_prema_tipu_krg]    Script Date: 3/31/2020 6:52:14 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [IO].[usp_ukupno_donacija_prema_tipu_krg]
@krg_id smallint
as
begin
declare @sum int
select @sum = [IO].[fn_broj_donacija_tip_krg](@krg_id)
print @sum
end;

这就是我得到的代码:

    public void DohvatiDonacijePoKG(string krgId)
    {
        SqlCommand command = new SqlCommand("[IO].[usp_ukupno_donacija_prema_tipu_krg]", cnn);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@krg_id", SqlDbType.VarChar)).Value = krgId;

        //return the output from DB

        try
        {
            command.ExecuteNonQuery();
            MessageBox.Show("Success.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Execption: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

无法为此搜索互联网找到合适的解决方案,如果我错过了,请对不起。

感谢您的帮助!

c# sql-server stored-procedures
1个回答
1
投票

一种方法是使用输出参数:

ALTER procedure [IO].[usp_ukupno_donacija_prema_tipu_krg]
@krg_id smallint, @sum INT OUT
as
begin
   set @sum = [IO].[fn_broj_donacija_tip_krg](@krg_id); 
end;

存储过程可以通过几种方式返回值:

© www.soinside.com 2019 - 2024. All rights reserved.