English 中文(简体)
OracleParameter and DBNull.Value
原标题:
  • 时间:2009-11-09 14:37:41
  •  标签:
  • c#
  • sql
  • oracle

we have a table in an Oracle Database which contains a column with the type Char(3 Byte).
Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work:

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,
but the circumstance that you would write a normal sql with "is null" and not "= null"
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?

Thanks

最佳回答

Null is the absence of being set to anything so you won t get the correct behavior with = 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." 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 . Then use an if statement to choose which one to use.

statement 1:

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

statement 2:

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

That is unless you re always comparing to null. Then, just use statement 2

问题回答

In this situation you have to use 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.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签