SQL Server 错误无效的列名“NaN”

发布于 2024-12-04 02:52:18 字数 382 浏览 2 评论 0原文

我有一点问题。我从程序的 SQL Server 数据库中获取一些值,用它们进行一些计算,然后将计算出的值写回数据库。但是,某些值给我“错误无效的列名'NaN'。”导致此问题的列确实没有任何独特之处。它们是与正常工作的色谱柱相同的浮动型色谱柱。

con.Open();

AddPhase1Calc = new SqlCommand("UPDATE Asset SET Make_Up_Depr = " + Make_Up_Depr + " WHERE Asset_ID = " + a, con);

AddPhase1Calc.ExecuteNonQuery();


con.Close();

我不确定这是否是足够的代码,但这是导致我出现问题的第一列。

I'm having a bit of an issue. I am getting some values from my program's SQL Server database, doing some calculations with them, and then writing the calculated values back to the database. However, some values are giving me "Error Invalid column name 'NaN'." There really isn't anything unique about the columns that are causing this issue. They are the same float type columns as the columns that are working perfectly.

con.Open();

AddPhase1Calc = new SqlCommand("UPDATE Asset SET Make_Up_Depr = " + Make_Up_Depr + " WHERE Asset_ID = " + a, con);

AddPhase1Calc.ExecuteNonQuery();


con.Close();

I'm not sure if this is enough code but this is the first column that is causing me the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇俏 2024-12-11 02:52:19

我猜测 Make_Up_Depr 是一个双精度值,并且在某些情况下计算是评估零除以零。这导致值为 NaN,并且 SQL 尝试将其计算为列名/别名(因为它不是双精度值...)

I'm going to guess Make_Up_Depr is a double and that the calculation is evaluating zero divided by zero in some cases. Which results in the value being NaN, and SQL is trying to evaluate it to a column name/alias (since it's not a double value...)

极度宠爱 2024-12-11 02:52:19

您需要在 Make_Up_Depr 值两边加上单引号:

AddPhase1Calc = new SqlCommand("UPDATE Asset SET Make_Up_Depr = '" + Make_Up_Depr +
    "' WHERE Asset_ID = " + a, con);

此外,您永远不应该以这种方式构建 SQL 语句。如果变量来自用户输入,则您很容易受到 SQL 注入攻击。您应该改用参数。

最后,此代码需要位于 using 块中:

using (SqlConnection con = whatever)
{
    con.Open();

    using (AddPhase1Calc = new SqlCommand("UPDATE Asset SET Make_Up_Depr = '" + 
      Make_Up_Depr + "' WHERE Asset_ID = " + a, con))
    {
        AddPhase1Calc.ExecuteNonQuery();
    }
}

You need to put single quotes around the Make_Up_Depr value:

AddPhase1Calc = new SqlCommand("UPDATE Asset SET Make_Up_Depr = '" + Make_Up_Depr +
    "' WHERE Asset_ID = " + a, con);

Also, you should never build a SQL statement that way. You're open to SQL injection attacks if the variable comes from user input. You should be using parameters instead.

Finally, this code needs to be in a using block:

using (SqlConnection con = whatever)
{
    con.Open();

    using (AddPhase1Calc = new SqlCommand("UPDATE Asset SET Make_Up_Depr = '" + 
      Make_Up_Depr + "' WHERE Asset_ID = " + a, con))
    {
        AddPhase1Calc.ExecuteNonQuery();
    }
}
落墨 2024-12-11 02:52:19

您的值之一,Make_Up_Depra 的计算结果为 NaN。由于这是一个字符串,SQL Server 认为您正在尝试通过该名称引用列。

One of your values, either Make_Up_Depr or a is evaluating to NaN. Since this is a string, SQL Server thinks you are trying to reference a column by that name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文