System.Data.OracleClient 被微軟宣告為過時不建議使用,是你知道我知道連獨眼龍都知道的事,硬要繼續用甚至會有效能懲罰。所以在維護舊專案時,看到還在用 System.Data.OracleClient 的程式,我都會順手換成 Managed ODP.NET。(若為 .NET 3.5 平台則只能用 Unmanaged ODP.NET)

近日踩到小鐵釘一根。

如下圖,程式原本使用 System.Data.OracleClient,執行正常:

改 using Oracle.ManagedDataAccess.Client 換用 Managed ODP.NET,出現型別轉換錯誤:

這才想起之前就研究過,呼叫 GetValue()、GetFieldType() 讀取 NUMBER 型別欄位,ODP.NET 會依 byte, short, long, single, double, decimal 的順序,使用第一個能完整容納精確數字的型別,與 System.Data.OracleClient 一律轉為 decimal 不同。在上述案例,改用 ODP.NET 後 dr["n"] 的型別為 double,硬用 (decmial) 轉型會出錯,修改為 Convert.ToDecimal() 後,問題排除。

以上經驗提供需要處理 System.Data.OracleClient 轉 ODP.NET 的同學參考。


Comments

Be the first to post a comment

Post a comment