SQL Server 错误无效的列名“NaN”
我有一点问题。我从程序的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜测 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...)
您需要在
Make_Up_Depr
值两边加上单引号:此外,您永远不应该以这种方式构建 SQL 语句。如果变量来自用户输入,则您很容易受到 SQL 注入攻击。您应该改用参数。
最后,此代码需要位于
using
块中:You need to put single quotes around the
Make_Up_Depr
value: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:您的值之一,
Make_Up_Depr
或a
的计算结果为NaN
。由于这是一个字符串,SQL Server 认为您正在尝试通过该名称引用列。One of your values, either
Make_Up_Depr
ora
is evaluating toNaN
. Since this is a string, SQL Server thinks you are trying to reference a column by that name.