简体   繁体   English

OracleParameter和DBNull.Value

[英]OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). 我们在Oracle数据库中有一个表,其中包含一列类型为Char(3 Byte)的列。
Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn't work: 现在,我们使用参数化的sql来选择带有DBNull.Value的某些行,但它行不通:

OracleCommand command = null;  
OracleDataReader dataReader = null;  

string sql = "select * from TEST_TABLE where COLUMN_1 = :COLUMN_1";  

try  
{  
    OracleConnection connection = (OracleConnection) dbConnection;  
    command = new OracleCommand( sql, connection );  

    OracleParameter param_1 = new OracleParameter( "COLUMN_1", OracleDbType.Char );  
    command.Parameters.Add( param_1 );

    param_1.Value = DbNull.Value;

    dataReader = command.ExecuteReader( );

    int recordCount = 0;
    while( dataReader.Read( ) == true )
    {
        recordCount++;
    }

    Console.WriteLine( "Count = " + recordCount ); // is 0
}
[...]  

Did i miss something? 我错过了什么? We definitly have some rows which contains a DBNull, 我们肯定有一些包含DBNull的行,
but the circumstance that you would write a 'normal' sql with "is null" and not "= null" 但是您将用“ is null”而不是“ = null”编写“普通” sql的情况
is also noticeable. 也很明显。

Can somebody explain this behaviour? 有人可以解释这种行为吗? How do you write a parameterized sql where you need a condition to check for a DBNull? 如何编写需要条件才能检查DBNull的参数化sql?

Thanks 谢谢

Null is the absence of being set to anything so you won't get the correct behavior with '= null'. Null是没有设置为任何东西的情况,因此使用'= null'不会获得正确的行为。 Because null is the absence of value, it is not meaningful to say "This variable that lacks any value has the same value as this other variable that lacks any value." 因为null表示没有值,所以说“没有任何值的变量与另一个没有任何值的变量具有相同的值”是没有意义的。 You can't have the same value of something else if you don't have a value. 如果您没有价值,那么您将无法拥有其他东西的相同价值。

One way to get around this is to create two sql statements, one that accepts the parameter and another with 'is null'. 解决此问题的一种方法是创建两个sql语句,一个sql语句接受参数,另一个以“ is null”表示。 Then use an 'if' statement to choose which one to use. 然后使用“ if”语句选择要使用的语句。

statement 1: 陈述1:

string sql = "select * from TEST_TABLE where COLUMN_1 = :COLUMN_1  

statement 2: 陈述2:

string sql = "select * from TEST_TABLE where COLUMN_1 is null

That is unless you're always comparing to null. 除非您始终将其与null进行比较。 Then, just use statement 2 然后,只需使用语句2

In this situation you have to use IS NULL : 在这种情况下,您必须使用IS NULL

string sql = "select * from TEST_TABLE where COLUMN_1 is null";

Performing any comparison with a null value in SQL will always result in an unknown result, which means you won't get any rows returned. 在SQL中使用空值执行任何比较将始终导致未知结果,这意味着您将不会返回任何行。

you can do this: 你可以这样做:

select *
from   TEST_TABLE
where  (COLUMN_1 = :COLUMN_1 and :COLUMN_1 Is Not Null) Or 
       (COLUMN_1 Is Null     and :COLUMN_1 Is Null)

Modify your code like this: 像这样修改您的代码:

OracleCommand command = null;  
OracleDataReader dataReader = null;  

string sql = "select * from TEST_TABLE where COLUMN_1 IS NULL"

try  
{  
    OracleConnection connection = (OracleConnection) dbConnection;  
    command = new OracleCommand( sql, connection );  

    dataReader = command.ExecuteReader( );

    int recordCount = 0;
    while( dataReader.Read( ) == true )
    {
        recordCount++;
    }

    Console.WriteLine( "Count = " + recordCount ); // is 0
}
SELECT t1.*
  FROM t1, (SELECT :s v FROM dual) tmp
 WHERE t1.s = tmp.v OR (t1.s IS NULL AND tmp.v IS NULL)

This would also do it. 这也可以做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM