2012-08-24 84 views
2

誰能這麼好心地告訴我爲什麼,從Oracle數據庫讀取時,我得到以下錯誤:無法從十進制轉換爲int32

Object of type 'System.Decimal' cannot be converted to type 'System.Int32'. 

它當我通過一個變量(oValue發生)類型對象成功能。我已經嘗試過演員,轉換,取消選中,截斷,幾乎整個沙包,沒有任何工作。這裏是代碼失敗的地方:

PropertyInfo property = Objects[0].GetType().GetProperty(sProperty); 
property.SetValue(Objects[0], oValue, null);  

任何幫助將不勝感激。

編輯:這是我到目前爲止已經試過:

(int)oValueConvert.ToInt32(oValue)Math.Truncate((decimal)ovalue)Decimal.Truncate((decimal)oValue)Decimal.ToInt32((decimal)oValue),並使用unchecked {}

+0

您可以發佈您嘗試投射的變體嗎? –

+0

很高興,我現在編輯問題 – Skulmuk

回答

3

認沽: -

property.SetValue(Objects[0],Decimal.ToInt32(Convert.ToDecimal(oValue)) , null); 
+0

從這裏,我得到'指定的演員表無效。' – Skulmuk

+0

順便說一句,什麼是oValue? – perilbrain

+0

一個對象。正在讀取的當前值是1. – Skulmuk

0

已存儲過程甲骨文整數類型和你傳遞的參數在代碼Ado.net中輸入十進制。

你可以把你的參數值

decimal value = ...; 
int convertedValue = Convert.ToInt32(value); 
0

由於物業預計的Int32你應該在Double值不傳遞給它。

裝箱值只能是拆箱完全相同的類型,所以拆箱它轉換爲整數和框它:

oValue = (int)((Decimal)oValue); 

如果能夠從開始使得該值爲Int32這當然會會更好。