1

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

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

任何解决此问题的建议。

4

1 回答 1

0

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/

也许您的数字太大而无法处理双打???

于 2013-09-20T11:53:22.023 回答