从SQL存储过程c#

时间:2016-04-27 14:54:16

标签: c# sql-server stored-procedures

我正在尝试从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

用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();
    }
}

事情是,我总是得到null作为结果。这是biztalk中xsl映射的Helper类。所有输入参数都直接来自地图。我想检查我们的数据库,是否存在这些客户端,以及是否存在使用它们的进程。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。问题出在2或3个地方。首先我在我的SQL存储过程中有错误,这是不通用的,所以我没有在这里发布它(它只适用于我的项目)。另一个错误是从存储过程转换输出:

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

其中processID被声明为“int?”和errorMsg声明为“字符串”。

感谢您的帮助。