如何避免“从类型'DBNull'转换为'Double'类型无效。”当数据库中没有值时? [英] How do I avoide "Conversion from type 'DBNull' to type 'Double' is not valid." when there is no values in the database?

查看:254
本文介绍了如何避免“从类型'DBNull'转换为'Double'类型无效。”当数据库中没有值时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免从类型'DBNull'转换为'Double'类型无效。当数据库中没有值时?我正在尝试使用

SQl从表名中选择sum(字段)where条件添加特定数据字段。

但是得到上面提到的错误。

解决方案

根据您查询的数据库类型,您可以修改您的查询以将任何空结果替换为0.

...类似于:



 选择 ISNULL(sum(field), 0 来自 tablename 


您可以使用Convert.IsDBNull方法 [ ^ ]。

Quote:

返回指示对象是否为 DBNull


How do I avoide "Conversion from type 'DBNull' to type 'Double' is not valid." when there is no values in the database? I am trying to add the perticular data field using
SQl select sum (field) from tablename where condition .
but getting the error mentioned above.

解决方案

Depending on the type of database your querying, you could modify your query to replace any null results with 0.
... something like:

select ISNULL(sum(field),0) from tablename 


You can use Convert.IsDBNull Method[^].

Quote:

Returns an indication whether the specified object is of type DBNull.


这篇关于如何避免“从类型'DBNull'转换为'Double'类型无效。”当数据库中没有值时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆