2016-09-21 91 views
0

IDE的Visual Studio 2015年
**。NET 「」 4.5
平臺的WinFormsDataRow.Field <T>(浮動欄)拋出無效轉換異常

我有一個數據庫表列[UsedSize] FLOAT。查詢此表並將值存儲在局部變量中時,我得到一個無效的轉換異常;示例代碼:

SqlDataAdapter sda = new SqlDataAdapter(cmdText, conn); 
DataTable dt = new DataTable(); 
sda.Fill(dt); 

foreach (DataRow row in dt.Rows) 
{ 
    InfoLibrary ir = new InfoLibrary(); 
    ir.LibraryID = row.Field<int>("LibraryID"); 
    ir.Name = row.Field<string>("Name"); 
    ir.UsedSize = row.Field<float>("UsedSize"); 
    ir.TotalSize = row.Field<float>("TotalSize"); 
    ir.TotalFileCount = row.Field<int>("TotalFileCount");     
    ilList.Add(ir); 
} 

當步進執行並在故障線路執行一個「快速觀看」我發現,通過改變線row.Field(「總計TOTALSIZE」),所述細胞類型的值是零。爲什麼會發生這種情況,在表格定義中,這很明顯,並且float

+0

嘗試使用雙,而不是 – SpaceghostAli

+2

根據[此線索](http://stackoverflow.com/questions/546150/float-in-database-to -in-net)DB'FLOAT'是一個C#'double'。 –

回答