简体   繁体   English

DataRow.Field <T> (浮动列)引发无效的强制转换异常

[英]DataRow.Field<T>(float Column) throws invalid cast exception

IDE Visual Studio 2015 IDE Visual Studio 2015年
**.NET"" 4.5 **。NET“” 4.5
Platform WinForms 平台WinForms

I have a database table with a column [UsedSize] FLOAT . 我有一个带有[UsedSize] FLOAT列的数据库表。 When querying this table and storing the values in local variables I get an invalid cast exception; 当查询该表并将值存储在局部变量中时,我得到一个无效的强制转换异常。 sample code: 样例代码:

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);
}

When stepping the execution and performing a "Quick Watch" on the faulty line I discovered that, by changing the line to row.Field("TotalSize"), the cell value type is zero. 当在错误的行上执行并执行“快速监视”时,我发现通过将行更改为row.Field(“ TotalSize”),单元格值类型为零。 Why would this happen when, in the table definition, this is clearly and float ? 当在表定义中这显然是float时,为什么会发生这种情况?

SQL Server浮点数应映射到.Net Double,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM