c# - 算术溢出问题

标签 c# asp.net sql-server-2005 types

我的数据库中有一个表作为空闲空间...它有一个名为 freesize 的列,其数据类型为 int。

我有三个值(value):

1065189988

1073741818

1073741819

现在我尝试获取总可用空间,但出现错误。

private void GetFreeSpace()
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = ConfigurationManager.ConnectionStrings["SumooHAgentDBConnectionString"].ConnectionString;
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("select sum(freesize)* 0.000000000931322575 as freespace from freespace", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dt);
        connection.Close();
        if (dt.Rows.Count > 0)
        {
          double  freeSpace = Convert.ToDouble(dt.Rows[0]["freespace"].ToString());
        }
    }

Arithmetic overflow error converting expression to data type int.

Line 123:            SqlCommand sqlCmd = new SqlCommand("select sum(freesize)* 0.000000000931322575 as freespace from freespace", connection);
Line 124:            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
Line 125:            **sqlDa.Fill(dt);** here i get error
Line 126:            connection.Close();
Line 127:            if (dt.Rows.Count > 0)

关于如何在不更改数据类型的情况下获取空闲空间中的值有什么建议吗?

谢谢

我知道它适用于 bigint,但我想要 int。

最佳答案

所以您要对 3 个大整数求和,然后乘以一个小的 double ,但仍希望输出为 int?

您可以将每一行检索为一个 int,然后在 C# 中进行数学运算

或者您可以在求和之前将 int 转换为 bigint 或 double,这样就不会在那里溢出。

关于c# - 算术溢出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2001631/

相关文章:

c# - 无法绑定(bind)到数据源上的属性或列名称。参数名称 : dataMember

c# - 将参数发送到 jQuery 的 Ajax 无效

sql-server-2005 - 是否可以通过事务复制获得低于 1 秒的延迟?

sql - 在存储过程中执行将获得存储过程的好处

c# - 数据绑定(bind)到 xaml 中对象列表中的对象列表中的对象列表

C# UWP 使用 Microsoft Edge 打开网址

c# - 无限数量的子弹

c# - 将文件附加到电子邮件而不创建文件

asp.net - 将旧的 asp 文件重定向到新的 aspx (asp.net) 文件(永久重定向,SEO)

sql - 列的创建和使用