简体   繁体   English

从SQL存储过程c#加载输出参数

[英]load Output parameters from SQL stored procedure c#

I am trying to load output parameters from my SQL stored procedure. 我试图从我的SQL存储过程加载输出参数。

USE [EdiMon_Beta]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetClients] 
-- Add the parameters for the stored procedure here
@sender nvarchar(max),
@subSender nvarchar(max),
@receiver nvarchar(max),
@subReceiver nvarchar(max),
@msgTypeID int,

@ErrorMsg nvarchar(max) = null OUTPUT,
@processId int = 0 OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE
@senderId int,
@subSenderId int,
@receiverId int,
@subReceiverId int



SELECT
    @senderId = ID
FROM [dbo].[Party_Client]
WHERE
    [Number] LIKE @sender
IF(@senderId IS NULL)
BEGIN
    SET @ErrorMsg = 'Sender does not exist'
    RETURN ;
END


SELECT
    @subSenderId = ID
FROM [dbo].[Party_Client]
WHERE
    [Number] LIKE @subSender
    AND ParentId = @senderId
    AND IsSubClient = 1
IF(@subSenderId IS NULL)
BEGIN
    SET @ErrorMsg = 'SubSender does not exist'
    RETURN ;
END


SELECT
    @receiver = ID
FROM [dbo].[Party_Client]
WHERE
    [Number] LIKE @receiver
IF(@receiverId IS NULL)
BEGIN
    SET @ErrorMsg = 'Receiver does not exist'
    RETURN;
END


SELECT
    @subReceiverId = ID
FROM [dbo].[Party_Client]
WHERE
    [Number] LIKE @subReceiver
    AND ParentId = @receiverId
    AND IsSubClient = 1
IF(@subReceiverId IS NULL)
BEGIN
    SET @ErrorMsg = 'SubReceiver does not exist'
    RETURN ;
END


SELECT @processId = ID FROM [dbo].[Party_Processes]
WHERE MsgTypeId = @msgTypeID
AND SenderId = @senderId
AND ReceiverId = @receiverId

END

And reading it with c# code: 用c#代码读取它:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    try
    {
        SqlCommand command = new SqlCommand("GetClients", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@sender", sender));
        command.Parameters.Add(new SqlParameter("@subSender", subSender));
        command.Parameters.Add(new SqlParameter("@receiver", receiver));
        command.Parameters.Add(new SqlParameter("@subReceiver", subReceiver));
        command.Parameters.Add(new SqlParameter("@msgTypeID", msgTypeID));

        var errorMsgParam = command.Parameters.Add(new SqlParameter("@ErrorMsg", SqlDbType.NVarChar, -1));
        errorMsgParam.Direction = ParameterDirection.Output;

        var processIdParam = command.Parameters.Add(new SqlParameter("@processId", SqlDbType.Int, -1));
        processIdParam.Direction = ParameterDirection.Output;

        connection.Open();
        command.ExecuteNonQuery();

        var processIDResult = command.Parameters["@processId"].Value;
        var errorMsgResult = command.Parameters["@ErrorMsg"].Value;

        processID = (int)processIDResult;
        errorMsg = errorMsgResult.ToString();
    }
}

The thing is, I always get null as result. 问题是,我总是得到null作为结果。 This is Helper class for xsl mapping in biztalk. 这是biztalk中xsl映射的Helper类。 All the input parameters are directly from the map. 所有输入参数都直接来自地图。 I want to check our database, if these clients exist and also, if process which using them exist. 我想检查我们的数据库,是否存在这些客户端,以及是否存在使用它们的进程。

thanks for your help :) 谢谢你的帮助 :)

OK, i solved this. 好的,我解决了这个问题。 Problem was in 2 or 3 places. 问题出在2或3个地方。 For first i had mistake in my SQL stored procedure which is not universal so i didnt post it here (it applies only to my project). 首先我在我的SQL存储过程中有错误,这是不通用的,所以我没有在这里发布它(它只适用于我的项目)。 The other mistake was in converting my output from stored procedure: 另一个错误是从存储过程转换输出:

processID = command.Parameters["@processId"].Value as int?;
errorMsg = command.Parameters["@ErrorMsg"].Value == DBNull.Value ? string.Empty : command.Parameters["@ErrorMsg"].Value.ToString();

where processID is declared as "int?" 其中processID被声明为“int?” and errorMsg is declared as "string". 和errorMsg声明为“字符串”。

Thanks for all the help. 谢谢你的帮助。

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

相关问题 具有输出参数的存储过程在C#中返回-1 - Stored procedure with output parameters returns -1 in c# 在 C# 中使用存储过程输出参数 - Using stored procedure output parameters in C# 如何在SQL Server中创建存储过程并通过C#代码使用该过程的一些参数从该过程中调用该过程 - How to create stored procedure in sql server and call that procedure from C# code behind with some parameters to that procedure 我从C#调用具有2个参数(输入和输出)的存储过程,但未显示输出参数的值 - I call a Stored procedure with 2 parameters (input and output) from C# but value of output parameter does not show 从C#中的存储过程检索输出 - Retrieving output from stored procedure in c# 将C#方法参数映射到sql存储过程参数 - mapping c# method parameters to sql stored procedure parameters 使用c#代码中的dapper将输出参数传递给存储过程 - Passing Output parameters to stored procedure using dapper in c# code 在 C# 中使用存储过程输出参数获取列值 - Get column values using stored procedure output parameters in C# 存储过程,输出参数连接到C#中的方法 - Stored Procedure with output parameters connected to method in C# 在C#中运行存储过程,传递参数并捕获输出结果 - Run stored procedure in C#, pass parameters and capture the output result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM