溢出异常发生:算术运算导致溢出

时间:2013-09-20 09:09:32

标签: c# oracle10g

我收到错误“算术运算导致溢出”。执行以下条件时

long countryCode = Convert.ToInt64(dr["Country_Code"]);
                    double curValue = Convert.ToDouble(dr["Conversion_Rate"]);
                    string selectSql = " SELECT NVL ((ABS ( " + curValue + " - AVG (conversion_rate)) / AVG (conversion_rate)) * 100,0) AS rate_deviation" +
                                       " FROM " + DBObjects.TABLE_ODS_CURRENCY_CONVERSIONS + " WHERE country_code = " + countryCode;

DataTable dtDeviation = this.ServerCfgReader.DefaultDBProvider.DBDataAccess.GetSqlSelection(selectSql);

当我在SQL中执行条件时,上面的查询得到算术运算导致溢出异常发生..

有任何解决此问题的建议。

1 个答案:

答案 0 :(得分:0)

Convert.ToInt64 ....改为使用double。

此外,当像这样构建SQL时,使用字符串构建器,它的效率会更高......

double countryCode = Convert.ToDouble(dr["Country_Code"]);
double curValue = Convert.ToDouble(dr["Conversion_Rate"]);

StringBuilder selectSql = new StringBuilder();
selectSql.AppendFormat(" SELECT NVL ((ABS ( {0} - AVG (conversion_rate)) / AVG (conversion_rate)) * 100,0) AS rate_deviation ",curValue );
selectSql.AppendFormat(" FROM {0} ", DBObjects.TABLE_ODS_CURRENCY_CONVERSIONS);
selectSql.AppendFormat(" WHERE country_code = {0}",countryCode);


DataTable dtDeviation = this.ServerCfgReader.DefaultDBProvider.DBDataAccess.GetSqlSelection(selectSql.ToString());

失败了,请阅读:http://www.dreamincode.net/forums/topic/89392-arithmetic-operation-resulted-in-an-overflow/

也许你的数字太大而无法像双打一样处理???