Oracle“ORA-01008:并非所有变量都绑定"带参数的错误
这是我第一次与 Oracle 打交道,我很难理解为什么会收到此错误.
This is the first time I've dealt with Oracle, and I'm having a hard time understanding why I'm receiving this error.
我在查询的 where 子句中使用带有 C# 的 Oracle 的 ODT.NET 和以下代码:
I'm using Oracle's ODT.NET w/ C# with the following code in a query's where clause:
WHERE table.Variable1 = :VarA
AND (:VarB IS NULL OR table.Variable2 LIKE '%' || :VarB || '%')
AND (:VarC IS NULL OR table.Variable3 LIKE :VarC || '%')
我正在添加参数值,如下所示:
and I'm adding the parameter values like so:
cmd.Parameters.Add("VarA", "24");
cmd.Parameters.Add("VarB", "test");
cmd.Parameters.Add("VarC", "1234");
当我运行这个查询时,服务器返回:
When I run this query, the server returns:
ORA-01008: not all variables bound
如果我注释掉任何一个AND(...."行,查询就会成功完成.
If I comment out either of the 'AND (....' lines, the query completes successfully.
如果我只用两个参数查询,而不用三个参数查询,为什么查询会正常运行?我收到的错误甚至没有意义
Why would the query run through alright if I'm only querying with two parameters, but not with three? The error I'm receiving doesn't even make sense
推荐答案
oracle 的 ODP.Net 提供程序默认使用按位置绑定.将行为更改为按名称绑定.将属性 BindByName 设置为 true.比你可以忽略参数的双重定义.
The ODP.Net provider from oracle uses bind by position as default. To change the behavior to bind by name. Set property BindByName to true. Than you can dismiss the double definition of parameters.
using(OracleCommand cmd = con.CreateCommand()) {
...
cmd.BindByName = true;
...
}
相关文章