无法通过C#代码将小数插入到sql server表中
我无法将十进制值插入到 Sql Server 表中。
我想做的通过这些代码行是不言自明的:
long fileSizeInBytes = ComponentUploadControl.FileContent.Length;
Decimal fileSizeInMB = Convert.ToDecimal(fileSizeInBytes) / (1024.0m * 1024.0m);
Decimal fileSizeInMBRounded = Math.Round(fileSizeInMB, 2);
cmd.Parameters.Add("@fileSize", SqlDbType.Decimal).Value = fileSizeInMBRounded;
插入数据库的值被去掉了小数位。更具体地说,如果 fileSizeInMBRounded 为 11.73,则插入数据库的值为 11,00
请帮助我,
谢谢您的期待
I'm not able to insert decimal values into Sql Server table.
What i'm trying to do is self explanatory through these lines of code:
long fileSizeInBytes = ComponentUploadControl.FileContent.Length;
Decimal fileSizeInMB = Convert.ToDecimal(fileSizeInBytes) / (1024.0m * 1024.0m);
Decimal fileSizeInMBRounded = Math.Round(fileSizeInMB, 2);
cmd.Parameters.Add("@fileSize", SqlDbType.Decimal).Value = fileSizeInMBRounded;
The value that is getting inserted into database is stripped of the decimal places. To be more specific if the fileSizeInMBRounded is 11.73, the value that is getting inserted into database is 11,00
Please help me
Thanks in anticipation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑你的小数字段在 SQL 中设置错误。您想要的是一个允许 2 位小数的
decimal(18,2)
字段。我怀疑您将该字段声明为decimal(18,0)
(默认值),它不允许任何小数位。如果您可以使用 SQL Server Profiler 来验证进入数据库的 INSERT 内容,那么您将更容易确定此类问题是由于您的代码还是 SQL Server 上的某些原因造成的。
My suspicion is that your decimal field is set up wrong in SQL. What you want is a
decimal(18,2)
field which allows for 2 decimal places. My suspicion is that you declared the field asdecimal(18,0)
(the default), which does not allow for any decimal places.If you can use SQL Server Profiler to verify the contents of the INSERT going to your DB, it would be easier for you to determine whether a problem like this is due to your code or something on the SQL server.
尝试设置 SqlParameter.Scale 2.
另请参阅 Bruno 建议的
Precision
属性(默认值为 0)。Try to set the SqlParameter.Scale to 2.
Also look into
Precision
property as suggested by Bruno (default is 0).您应该设置比例和精度。
我认为这就是问题所在。
You should set Scale and Precision.
I think that is the problem.
你的参数看起来不错。我会检查数据库列数据类型
Your parameter looks fine. I would check the data base column data type