我的数据库中有一个表作为空闲空间...它有一个名为 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/