Oracle Data Provider and casting

Posted by mrjoltcola on Stack Overflow See other posts from Stack Overflow or by mrjoltcola
Published on 2010-03-23T23:10:10Z Indexed on 2010/03/23 23:13 UTC
Read the original article Hit count: 417

Filed under:
|
|

I use Oracle's specific data provider, not the Microsoft provider that is being discontinued. The thing I've found about ODP.NET is how picky it is with data types. Where JDBC and other ADO providers just convert and make things work, ODP.NET will throw an invalid cast exception unless you get it exactly right.

Consider this code:

        String strSQL = "SELECT DOCUMENT_SEQ.NEXTVAL FROM DUAL";

        OracleCommand cmd = new OracleCommand(strSQL, conn);
        reader = cmd.ExecuteReader();
        if (reader != null && reader.Read()) {
           Int64 id = reader.GetInt64(0);
           return id;
        }

Due to ODP.NET's pickiness on conversion, this doesn't work. My usual options are:

1) Retrieve into a Decimal and return it with a cast to an Int64 (I don't like this because Decimal is just overkill, and at least once I remember reading it was deprecated...)

Decimal id = reader.GetDecimal(0);
return (Int64)id;

2) Or cast in the SQL statement to make sure it fits into Int64, like NUMBER(18)

String strSQL = "SELECT CAST(DOCUMENT_SEQ.NEXTVAL AS NUMBER(18)) FROM DUAL";

I do (2), because I feel its just not clean pulling a number into a .NET Decimal when my domain types are Int32 or Int64. Other providers I've used are nice (smart) enough to do the conversion on the fly.

Any suggestions from the ODP.NET gurus?

© Stack Overflow or respective owner

Related posts about oracle11g

Related posts about odp.net