Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SqlClient Xml输出参数";“未提供”;_C#_Sql Server_Xml_Parameters_Sqlclient - Fatal编程技术网

C# SqlClient Xml输出参数";“未提供”;

C# SqlClient Xml输出参数";“未提供”;,c#,sql-server,xml,parameters,sqlclient,C#,Sql Server,Xml,Parameters,Sqlclient,运行以下代码时,我收到一个SqlException 过程或函数“usp_Search”需要参数“@pxmlSearchOutput”,但未提供该参数 我的参数+请求 using (var connection = new SqlConnection(_connectionString)) { using (var command = new SqlCommand("Search.usp_Search", con)) { v

运行以下代码时,我收到一个SqlException

过程或函数“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 profiler,我可以提取以下内容:

        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',
        @pxmlSearchOutput=@p3 output,
        @pxmlSearchInput=@p4,
        @pbitDebug=0,
        @pintErrorNumber=@p6 output
        select @p3, @p6

我无法准确地诊断SQL有什么问题(以及它与.net代码的关系)。有什么想法吗?

您执行批处理,类型为:

您确实向该批处理传递了一组参数,但这些参数都被忽略,因为它们实际上没有在批处理本身中使用。你有两个选择:

  • 使用批本身中传递给批的参数:
    var command=new SqlCommand(“exec Search.usp_Search@pxmlSearchOutput,@pxmlSearchInput,@pbitDebug,@pintErrorNumber output”,con))
  • 告诉ADO.NEt您正在进行RPC调用,而不仅仅是执行批处理:
    command.CommandType=CommandType.StoredProcess

任何一种改变都会起作用(但不是两者都起作用)。

Genius!谢谢你的帮助。我添加了command.CommandType=STP,一切都是金色的。哎哟,我想知道为什么这对我不起作用,但当然,我忘记了命令类型:\Thank;)
Search.usp_Search