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

本文关键字:溢出 算术运算 异常 | 更新日期: 2023-09-27 18:32:02

执行以下条件时出现错误"算术运算导致溢出"。

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中执行条件时,上面的查询得到了算术运算,导致发生溢出异常

解决此问题的任何建议。

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

Convert.ToInt64....改用双精度。

此外,当像这样构建 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/

也许你的数字太大了,无法作为双打处理???