ExecuteNonQuery返回随机数而不是返回实际行

本文关键字:返回 随机数 ExecuteNonQuery | 更新日期: 2023-09-27 18:34:40

我有将xml作为输入的存储过程。

执行 SP 的 C# 代码是

using (SqlConnection conn = new SqlConnection(connString))
{
    try
    {
        conn.Open();
        SqlCommand command = new SqlCommand("sp_Configure_Users", conn);
        command.CommandType = CommandType.StoredProcedure;
        var xmlData = new SqlParameter(parameterName: "@XMLDATA", value: GenerateXML(_userDetails));
        command.Parameters.Add(xmlData);
        count = command.ExecuteNonQuery();
        if (count > 0)
        {
            return count;
        }
        else
        {
            return 0;
        }
    }
    catch (SqlException  )
    {
    }
}

该方法 生成 XML 返回一个字符串,该字符串采用 xml 数据的形式。在我存储的进程中,我已经删除了 SET NOCOUNT ON 以在插入、删除或更新时返回我的实际行数。如果我使用示例 xml 数据运行查询:-

<UserCollection>
    <InsertList><Users User_Id="438"  First_Name="Praveen"  Middle_Name=""  Last_Name="Kumar"  Designation_Id="1"  Email="" Contact_Number="96533"  Updated_By="pkumar"  />
    </InsertList>
    <UpdateList></UpdateList>
    <DeleteList></DeleteList>
</UserCollection>

返回的值是 3 而不是 1,因为在我的集合中,我只传递需要插入的一行。由于没有更新或删除的行。当 SP 必须只返回 1 时,它怎么能返回 3

存储的 Proc 是

  set ANSI_NULLS ON
  set QUOTED_IDENTIFIER ON
   go

  ALTER PROCEDURE [dbo].[sp_Configure_Users]
@XMLDATA xml
  AS
 DECLARE @Users_Staging Table(
[User_Id] [int] NOT NULL,
[First_Name] [varchar](200) NOT NULL,
[Middle_Name] [varchar](200) NULL,
[Last_Name] [varchar](200) NOT NULL,
[Designation_Id] [int] NOT NULL,
[Email] [varchar](250) NOT NULL,
[Contact_Number] [varchar](50) NOT NULL,
[Updated_By] [varchar](255) NOT NULL
  )
  BEGIN
  Set nocount off
  Go    
  BEGIN TRY
  BEGIN TRANSACTION

  DECLARE @CurrDate datetime;
  SELECT @CurrDate = GETDATE();
   -- Insert statements for procedure 
  INSERT INTO Users
  ([User_Id]
  ,[First_Name]
  ,[Middle_Name]
  ,[Last_Name]
  ,[Designation_Id]
  ,[Email]
  ,[Contact_Number]
  ,[Updated_By]
  ,[Updated_Date]
  )
  Select    
XMLDATA.item.value('@User_Id[1]', 'int') AS User_Id,    
XMLDATA.item.value('@First_Name[1]', 'varchar(200)') AS First_Name,
XMLDATA.item.value('@Middle_Name[1]', 'varchar(200)') AS Middle_Name,
XMLDATA.item.value('@Last_Name[1]', 'varchar(200)') AS Last_Name,
XMLDATA.item.value('@Designation_Id[1]', 'int') AS Designation_Id,
XMLDATA.item.value('@Email[1]', 'varchar(250)') AS Email,
XMLDATA.item.value('@Contact_Number[1]', 'varchar(50)') AS Contact_Number,
XMLDATA.item.value('@Updated_By[1]', 'varchar(255)') AS Updated_By,
@CurrDate
  FROM @XMLDATA.nodes('//UserCollection/InsertList/Users') AS XMLDATA(item)
  -- Update statements for procedure 
  INSERT INTO @Users_Staging
       ([User_Id]
  ,[First_Name]
  ,[Middle_Name]
  ,[Last_Name]
  ,[Designation_Id]
  ,[Email]
  ,[Contact_Number]
  ,[Updated_By])
Select  
XMLDATA.item.value('@User_Id[1]', 'int') AS User_Id,    
XMLDATA.item.value('@First_Name[1]', 'varchar(200)') AS First_Name,
XMLDATA.item.value('@Middle_Name[1]', 'varchar(200)') AS Middle_Name,
XMLDATA.item.value('@Last_Name[1]', 'varchar(200)') AS Last_Name,
XMLDATA.item.value('@Designation_Id[1]', 'int') AS Designation_Id,
XMLDATA.item.value('@Email[1]', 'varchar(250)') AS Email,
   XMLDATA.item.value('@Contact_Number[1]', 'varchar(50)') AS Contact_Number,
XMLDATA.item.value('@Updated_By[1]', 'varchar(255)') AS Updated_By
  FROM @XMLDATA.nodes('//UserCollection/InsertList/Users') AS XMLDATA(item)

  UPDATE Users 
  SET First_Name=stgusr.First_Name,
  Middle_Name=stgusr.Middle_Name,
  Last_Name=stgusr.Last_Name,
  Email=stgusr.Email,
  Contact_Number=stgusr.Contact_Number,
  Updated_By=stgusr.Updated_By,
  Updated_Date=@CurrDate
  FROM Users usr INNER JOIN
  @Users_Staging stgusr
  ON usr.User_Id=stgusr.User_Id
-- Delete statements for procedure 
   DELETE Users
   WHERE User_Id
     IN (Select         
    XMLDATA.item.value('@User_Id[1]', 'int') AS User_Id
FROM @XMLDATA.nodes('//UserCollection/DeleteList/Users') AS XMLDATA(item))
   COMMIT TRANSACTION;
   END TRY
   BEGIN CATCH
--EXEC sp_rethrow_error;
ROLLBACK TRANSACTION;
    END CATCH; 
   END

ExecuteNonQuery返回随机数而不是返回实际行

如果存储过程更新/插入的表上有触发器,它们将影响受影响的行计数:

对于 UPDATE、INSERT 和 DELETE 语句,返回值为 受命令影响的行数。当触发器存在于 正在插入或更新的表,返回值包括数字 受插入或更新操作和数量影响的行数 受一个或多个触发器影响的行数。对于所有其他类型的 语句,返回值为 -1。如果发生回滚,则返回 值也是 -1。
裁判。

建议发布所涉及的表的存储过程和架构/触发器。