简体   繁体   中英

ODP.NET delete statement can not pass DBNull as parameter value

Using: .NET 4, ODP.NET 11.2.0.3.0, Oracle Database 10g Release 10.2.0.3.0

I am going crazy in trying to determine why my delete statement doesn't work. Perhaps someone here can help me.

Here is the code that DOES work:

cmd = New OracleCommand("delete from u_parameters where pkey = :pkey and user_id = :user_id and computer is null", Con)
cmd.Parameters.Add("pkey", OracleDbType.NVarchar2).Value = "Test"
cmd.Parameters.Add("user_id", OracleDbType.Decimal).Value = 1
cmd.ExecuteNonQuery()  ' -- this returns 1 as it should

Here is the code that DOESN'T work:

cmd = New OracleCommand("delete from u_parameters where pkey = :pkey and user_id = :user_id and computer = :computer", Con)
cmd.Parameters.Add("pkey", OracleDbType.NVarchar2).Value = "Test"
cmd.Parameters.Add("user_id", OracleDbType.Decimal).Value = 1
cmd.Parameters.Add("computer", OracleDbType.NVarchar2).Value = DBNull.Value
cmd.ExecuteNonQuery()  ' -- this returns 0!!

By doesn't work I mean that the statement executes but nothing happens in the database (and the result of ExecuteNonQuery is 0 which means no rows where affected). I really don't understand what could be the problem here. I've tried setting the 'computer' parameter IsNullable to True but it doesn't not change anything.

Please help.

AND x = NULL doesn't equate to true when x is null.

Try using IS NULL or the isnull() function.

See Not equal <> != operator on NULL for more info

You can do the following for any nullable parameters.

  oleDBCmd.Parameters.Add(new OracleParameter("computer", OracleType.NVarChar));`

if(string.IsNullOrEmpty(toStr)) {
    oleDBCmd.Parameters["computer"].Value = DBNull.Value;
} else {
    oleDBCmd.Parameters["computer"].Value = toStr;
}

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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