2010-01-12 30 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