質問

I am having issues comparing null values with oracle 11g odp.net driver. It works fine with oracle 10g odp.net driver. If a column is null in the database, then in datarow it has a string value of null.

This code fails:

int parentId =  row[PARENTID] != DBNull.Value ? int.Parse(row[PARENTID].ToString()) : 0;

Thanks

役に立ちましたか?

解決

Here :

var para6 = new OracleParameter("pOrganizationParentId", OracleDbType.Long){
    IsNullable = true,
};

if (string.IsNullOrEmpty(organizationParentId)) {
    para6.Value = null; 
} else {
    para6.Value = long.Parse(organizationParentId);
}

他のヒント

This has something to do with the ORacle11g. Like what you said, instead of real null value, it is a null string. If you are checking from OracleDbType, the null in OracleDbType and CLR Datatype both inherits from INullable so below was my solution:

((INullable) rowParamDBType.Value).IsNull

To have a cleaner solution, you can place this in extension.

If your row is a DbType, you can check for param.Value==DbNull.Value.


While the above will work too, the real solution to this problem is use the OracleDbTypeEx instead of OracleDbType in your OracleParameter declaration. OracleDbTypeEx will return the value to DBType and because of this it will recognize the DBNull. See example code below.

command.Parameters.Add(new OracleParameter
                            {
                                ParameterName = "param_out",
                                OracleDbTypeEx = OracleDbType.Decimal,
                                Direction = ParameterDirection.Output
                            });

if (command.Parameters["param_out"].Value != Convert.DBNull)
{
  //your code here
}

Fromm the Oracle documentation:

f the value is a DbType, you can check for param.Value==DbNull.Value

If the value is an OracleDbType, you can check for ((INullable)param.Value).IsNull since Oracle Types inherit INullable interface.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top