- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个用 C# 编写的 asp.net 应用程序。我有一个插入按钮,它接受用户在页面上输入的信息,将值添加到列表中,然后将列表发送到将数据插入到 MSSQL 数据库表中的方法。除了在后端将数据类型设置为“ float ”的字段外,一切都按预期工作。如果我在“温度”字段的文本框中输入诸如“70.3”的值,则插入的值实际上变为“70.3000030517578”。我还有一个调用更新方法的按钮。如果我将“70.3000030517578”改回“70.3”并将其发送到更新方法,则 70.3 会正确插入到数据库中。这是我的 SqlCommand 参数代码:
cmd.Parameters.AddWithValue("@temp", listIn[0].Temperature == null ? (object)DBNull.Value : listIn[0].Temperature);
上述行运行后,我在 Debug模式下查看参数,确认Value为70.3。见截图:
两个区别在于 DbType 和 SqlDbType。所以我尝试了下面的行,它没有改变任何东西(仍然添加额外的小数位):
cmd.Parameters.Add("@temp", SqlDbType.Float).Value = listIn[0].Temperature == null ? (object)DBNull.Value : listIn[0].Temperature;
如果我将后端的数据类型更改为 decimal(18,3),则插入方法会正确插入“70.300”。我唯一的选择是使用十进制数据类型而不是 float 吗?如果可能的话,我们更愿意使用 float。欢迎任何想法。我这样做的时间不长,所以如果我的问题不够详细,我深表歉意。
最佳答案
了解 Using decimal, float, and real Data
Approximate numeric data types do not store the exact values specified for many numbers; they store an extremely close approximation of the value. For many applications, the tiny difference between the specified value and the stored approximation is not noticeable. At times, though, the difference becomes noticeable. Because of the approximate nature of the float and real data types, do not use these data types when exact numeric behavior is required, such as in financial applications, in operations involving rounding, or in equality checks. Instead, use the integer, decimal, money, or smallmoney data types.
关于c# - SQL INSERT with ExecuteNonQuery() 插入额外的小数位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31664100/
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我在 asp.net mvc 中有一个 webapp,带有一个通过 ODBC 驱动程序链接的 mysql 数据库。 在旧环境中一切都运行良好,但现在我必须转移到新服务器(新操作系统、新 IIS、新 m
我不是一个很好的程序员,但我认为代码是正确的。任何人都可以检查它是否有错误,因为我总是在 executenonquery 行出错。 错误是: {"Incorrect syntax near '9'."
我正在尝试编写一个通用的保存函数,并且我正在使用 Dbcommand。我的代码是: private static int Save(CommandType commandtype, string co
您好,我正在尝试使用 ADO.NET 将数据插入数据库。这是我的代码。这是我的 C# 代码: public void CreateBook(FormCollection collection)
我有以下代码,它没有更新数据库。该代码正在执行它应该执行的操作,但是当我执行 ExecuteNonQuery() 时,它不会将任何内容保存回数据库。连接字符串没有问题,因为它与正在运行的选择查询相同。
我正在使用 ExecuteNonQuery 在数据库中插入一条记录并返回新记录的 ID。它插入正常,但输出参数未更新。 尝试在 SSMS 中执行存储过程并且运行良好。只有在使用 ExecuteNonQ
我认为连接没有问题,因为当我打开它时,它不会抛出任何错误。所以我猜错误是在我执行命令时出现的。这是我的代码: OleDbCommand cmd = new OleDbCommand("SELECT *
我正在使用 OracleCommand.ExecuteNonQuery() 插入到表中。一切正常,但偶尔没有插入记录。那么 ExecuteNonQuery() 是否有可能不插入记录也不抛出异常? 我没
我有最简单的代码... // Connect to database etc string sqlStr = "INSERT INTO .... ... "; MySqlCom.CommandText
我想检查数据库中的所有表并删除具有特定 id 的所有行。当行数较小时,效果很好。 在特定表中,行数为 900000,这不再起作用。 这是我当前的代码: Using connection = New M
这是我的代码: Document doc = new Document(); SqlConnection cnn = Ketnoi.GetSqlConnection(); cnn.Open(); Sq
我有这段代码,运行时给出此错误ExecuteNonQuery:Connection属性尚未初始化。 而且我有sql数据库。 它的名字叫Cost。 我有这段代码,运行时给出此错误ExecuteNonQu
我的sql server 2008数据库中有一个表 COMMENT(idcom,content,username); 该表有2条记录,其中username = lecturer 现在,我要删除所有具有
这是我的代码: private void loadlist() { comboBox1.Items.Clear(); cn.Open(); cmd.Co
我的代码在下面的 ExecuteNonQuery 行超时。我不知道为什么。我已将超时时间增加到 900,但它仍然超时。 using (var conn = new SqlConnection(loca
我正在使用 Microsoft Data-Tier Application framework基于 DacPackage 创建部署脚本目的。我正在尝试使用 Microsoft.SqlServer.Ma
我正在使用基于 WinForms 的 C# 工具,它有一个附加的基于 MDF 文件的数据库。我正在尝试使用 SqlCommand.ExecuteNonQuery() 方法将记录保存到这个附加的 MDF
我存储的过程如下: ALTER PROC pr_Update_Users_Nomination ( @UserID AS VARCHAR(100), @Nominated A
我正在尝试在 C# 中的循环内插入一条数据库记录。 当我像这样对值进行硬编码时它会起作用: string query3 = "INSERT INTO furniture (room_id,me
我是一名优秀的程序员,十分优秀!