2010-01-12 31 views
1

運行以下代碼時收到SqlException。SqlClient Xml輸出參數「未提供」

「過程或函數'usp_Search'需要參數'@pxmlSearchOutput',它沒有提供。」

我的參數+請求。

using (var connection = new SqlConnection(_connectionString)) 
    { 
     using (var command = new SqlCommand("Search.usp_Search", con)) 
     { 

      var pxmlSearchOutput = new SqlParameter(); 
      pxmlSearchOutput.ParameterName = "@pxmlSearchOutput"; 
      pxmlSearchOutput.SqlDbType = SqlDbType.Xml; 
      pxmlSearchOutput.Direction = ParameterDirection.Output; 
      pxmlSearchOutput.Size = 1; 
      command.Parameters.Add(pxmlSearchOutput); 

      var pxmlSearchInput = new SqlParameter(); 
      pxmlSearchInput.ParameterName = "@pxmlSearchInput"; 
      pxmlSearchInput.Value = requestXML;//is valid xml, is a local var 
      pxmlSearchInput.SqlDbType = SqlDbType.Xml; 
      command.Parameters.Add(pxmlSearchInput); 

      var pbitDebug = new SqlParameter(); 
      pbitDebug.Value = false; 
      pbitDebug.ParameterName = "@pbitDebug"; 
      pbitDebug.SqlDbType = SqlDbType.Bit; 
      command.Parameters.Add(pbitDebug); 

      var pintErrorNumber = new SqlParameter(); 
      pintErrorNumber.ParameterName = "@pintErrorNumber"; 
      pintErrorNumber.SqlDbType = SqlDbType.Int; 
      pintErrorNumber.Direction = ParameterDirection.Output; 
      command.Parameters.Add(pintErrorNumber); 

      connection.Open(); 
      command.ExecuteScalar(); 
      connection.Close(); 
     } 
    } 

使用SQL事件探查,我可以提取如下:

 declare @p3 xml 
     set @p3=null 
     declare @p4 xml 
     set @p4=convert(xml,'***Redacted - This is however, valid xml, which convert works on happily***') 
     declare @p6 int 
     set @p6=NULL 
     exec 
     sp_executesql 
     N'Search.usp_Search', 
     N'@pxmlSearchOutput xml output,@pxmlSearchInput xml,@pbitDebug bit,@pintErrorNumber int output', 
     @[email protected] output, 
     @[email protected], 
     @pbitDebug=0, 
     @[email protected] output 
     select @p3, @p6 

我無法診斷到底什麼是錯的SQL(因此,它是如何涉及到.NET代碼)。有任何想法嗎?

回答

2

您執行批處理,Text類型的請求:

Search.usp_Search 

你做通了一堆參數,此批,但實際上並沒有在該批次本身所使用的那些都被忽略。你有兩個選擇:

  • 使用的參數傳遞給一批批本身:var command = new SqlCommand("exec Search.usp_Search @pxmlSearchOutput output, @pxmlSearchInput,@pbitDebug, @pintErrorNumber output", con))
  • 告訴ADO.NEt你正在RPC調用,而不僅僅是執行批處理:command.CommandType = CommandType.StoredProcedure;

任何更改都可以使用(但不能同時使用)。

+0

天才!謝謝你的幫助。我添加了command.CommandType = STP,一切都是金色的。 – Gregory 2010-01-12 23:50:35

+1

哎呦是想知道爲什麼這也不適合我,但是當然,忘記了命令類型:\謝謝;) – Rabid 2011-07-04 15:12:06