繁体   English   中英

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

[英]Overflow Exception Occured: Arithmetic operation resulted in an overflow

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

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...。请改用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/

也许您的数字太大,无法像Doubles一样处理???

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM