将xml作为参数传递给SQL Server中的存储过程

本文关键字:Server 存储过程 SQL xml 参数传递 | 更新日期: 2023-09-27 18:08:06

我正在尝试将XML参数从c#代码传递到SQL Server中的存储过程,但它不起作用。

public static void SaveReceiptTrans(string pSiteCode, string xml)
{
        try
        {
            string DBKey = "sn";
            string ConnStr = Encryption.DecryptString(ConfigurationManager.ConnectionStrings[DBKey].ConnectionString);
            string CmdStr = "prc_pt_Fac_iu";
            SqlParameter[] SqlParams = new SqlParameter[1];
            SqlParams[0]            = new SqlParameter("@pReceiptsWithFactoryNameCode", SqlDbType.Xml);
            SqlParams[0].Direction  = ParameterDirection.Input;
            SqlParams[0].Value      = xml;
            int i = SqlHelper.ExecuteNonQuery(ConnStr, CommandType.StoredProcedure, CmdStr, SqlParams);
        }
        catch (Exception ex)
        {
            UtilLogging.LogException(ex, "Error From -> ", pSiteCode);
        }
    }

但是没有通过存储过程对表进行操作,就好像没有对存储过程进行调用一样。我已经用虚拟xml字符串测试了存储过程,它工作得很好,但当从c#代码传递参数时,它不起作用。

ALTER PROCEDURE [dbo].[prc_pt_Fac_iu]
(
    @pReceiptsWithFactoryNameCode XML
)
BEGIN
   SET NOCOUNT ON
   BEGIN TRY    
      DECLARE @MAXTRANSID INT, @MAXMOVEID INT, @ROWCOUNT INT, @NEXTTRANSID INT, @NEXTMOVEID INT
      DECLARE @IDOC INT
      DECLARE @TEMPCOUNT INT, @INTFLAG INT
      SELECT 
         IDENTITY(INT) AS id, 
         line.item.value('@site_cde', 'char(8)') AS site_cde,  
         line.item.value('@po_nbr', 'char(17)') AS po_nbr,  
         line.item.value('@po_line_nbr', 'smallint') AS po_line_nbr, 
         line.item.value('@ran', 'int') AS ran, 
         line.item.value('@factory_name_code', 'int') AS factory_name_code, 
         NULL AS item_id, 
         NULL AS qty,
         NULL AS cur_loc_id,
         NULL AS cur_loc_status,
         NULL AS trans_id,
         NULL AS [user_id]
      INTO 
         #tmpReceiptsWithFactoryNameCode  
      FROM 
         @pReceiptsWithFactoryNameCode.nodes('/POLines/POLine') AS line(item) 
      SELECT  
         @MAXTRANSID = COALESCE(MAX(trans_id),0)
      FROM 
         PartsTrack_Receipt_Trans (NOLOCK) 
      WHERE 
         trans_id IS NOT NULL
      UPDATE rt 
      SET trans_id = @MAXTRANSID + temp.id,
          factory_name_code = temp.factory_name_code
      FROM PartsTrack_Receipt_Trans_BKP rt 
      INNER JOIN #tmpReceiptsWithFactoryNameCode temp ON rt.receipt_ack_nbr = temp.ran 
                                                      AND rt.po_nbr = temp.po_nbr 
                                                      AND rt.po_line_nbr = temp.po_line_nbr
END
public static int ExecuteNonQuery(string pConnectionString, CommandType pCommandType, string pCommandText, SqlParameter[] pSqlParameters)
{
        SqlConnection lSqlConnection = null;
        try
        {
            lSqlConnection = new SqlConnection(pConnectionString);
            SqlCommand lSqlCommand = new SqlCommand();
            lSqlCommand.CommandType = pCommandType;
            lSqlCommand.Connection = lSqlConnection;
            lSqlCommand.CommandText = pCommandText;
            lSqlCommand.CommandTimeout = 180;
            if (pSqlParameters != null)
            {
                foreach (SqlParameter lSqlParameter in pSqlParameters)
                    lSqlCommand.Parameters.Add(lSqlParameter);
            }
            lSqlConnection.Open();
            return lSqlCommand.ExecuteNonQuery();
        }
        catch (SqlException sqlExp)
        {
            throw sqlExp;
        }
        catch (Exception exp)
        {
            throw exp;
        }
        finally
        {
            if (lSqlConnection != null && lSqlConnection.State != ConnectionState.Closed)
                lSqlConnection.Close();
        }
    }

我搜索了一下,发现这是传递xml参数的正确方法。

那我做错了什么?

感谢

编辑:

我发现了问题。所有建议的方法都是正确的,但由于xml是区分大小写的,我发现我传递的xml节点不匹配。

将xml作为参数传递给SQL Server中的存储过程

尝试用SqlXml传递XML值。

您需要传入变量xml作为SqlXml类的实例。您可以这样做:

SqlParams[0].Value = new SqlXml(new MemoryStream(Encoding.UTF8.GetBytes(xml)));