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

时间:2014-06-10 07:11:36

标签: c# sql xml

我正在尝试将C#代码中的XML参数传递给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节点中的错误匹配正在传递。

2 个答案:

答案 0 :(得分:2)

尝试传递XML值with SqlXml。然后它应该工作。

答案 1 :(得分:0)

您需要将变量xml作为SqlXml类的实例传入。您可以执行此操作,如下所示:

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