从包含NULL值的SQL Server数据库中获取float字段

时间:2013-02-04 10:10:50

标签: c# sql-server-2008 c#-3.0

我想从sql server database 2008 R2中获取浮点数据。

在数据库中有两个字段estimated_amountactual_amount

首先,只有estimated-amount已填充包含值,而actual_amount字段包含NULL

问题在于,当我获取数据并解析该值时,它显示错误:

  

用户代码Message = Input未处理System.FormatException   字符串格式不正确。源= mscorlib程序

我的代码是:

CRM_Doctor_RequestObj.Actual_Amount = float.Parse(Convert.ToString(row["Actual_Amount"]));

请建议我能做些什么..

2 个答案:

答案 0 :(得分:1)

从数据库中选择actual_amount,如

SELECT ISNULL((actual_amount),'0.00') FROM TableName

如果actual_amount为null,则它给出0.00

答案 1 :(得分:1)

编写更少的代码并避免转换为字符串。您必须单独处理null - 根据row的内容,它可能支持IsNull / IsDBNull方法,因此更像是:

if(row.IsNull("Actual_Amount"))
   CRM_Doctor_RequestObj.Actual_Amount = null;
else
   CRM_Doctor_RequestObj.Actual_Amount = (float)row["Actual_Amount"];
相关问题