SqlClient Xml 输出参数“未提供”
运行以下代码时,我收到 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 代码的关系)。有什么想法吗?
I receive an SqlException when running the following code.
"Procedure or function 'usp_Search' expects parameter '@pxmlSearchOutput', which was not supplied."
My parameters + request.
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();
}
}
Using sql profiler, I can extract the following:
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
I am unable to diagnose exactly what is wrong with the SQL (and thus, how it relates to the .net code). Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您执行批处理,即 Text 类型的请求:
您确实向该批处理传递了一堆参数,但这些参数都被忽略,因为它们实际上并未在批处理本身中使用。您有两种选择:
var command = new SqlCommand("exec Search.usp_Search @pxmlSearchOutput output, @pxmlSearchInput,@pbitDebug, @pintErrorNumber output", con))< /code>
command.CommandType = CommandType.StoredProcedure;
任一更改都有效(但不能同时执行)。
You execute the batch, a request of type Text:
You do pass a bunch of parameters to this batch, but those are all ignored as they aren't actually used in the batch itself. You have two alternatives:
var command = new SqlCommand("exec Search.usp_Search @pxmlSearchOutput output, @pxmlSearchInput,@pbitDebug, @pintErrorNumber output", con))
command.CommandType = CommandType.StoredProcedure;
Either change will work (but not both).