I use Oracle's specific data provider (11g), 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开发者_Python百科 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?
This will work with your original SQL:
Int64 id = System.Convert.ToInt64(reader.GetValue(0));
精彩评论