我想根据数据库中的折扣修改金额 HT 价格。
但是我在计算折扣后的净价时遇到了问题。
我有这个错误
操作数 nvarchar 的数据类型对乘法运算符无效
这是我的代码:
comm.CommandText = "UPDATE F_DOCLIGNE SET DL_DateBC = @date,
DL_Remise01REM_Valeur = @remise, DL_PrixUnitaire = @pu, DL_MontantHT =
(@remise * @pu) / 100 where AR_Ref = @code";
comm.Parameters.AddWithValue("@date", textBox_livr.Text);
comm.Parameters.AddWithValue("@pu", textBox_prix.Text);
comm.Parameters.AddWithValue("@code", textBox_art.Text);
comm.Parameters.AddWithValue("@remise", textBox_rem.Text);
DL_Remise01REM_Valeur = @remise is the discount
DL_MontantHT = (@remise * @pu) / 100 is my price calculate with the discount
正如戈登和斯图尔特在评论中所写的那样,问题在于您使用的是AddWithValue
.
使用时AddWithValue
,编译器必须从值(和元数据,如果存在)推断参数的数据类型。
如果您使用的是内联 sql,则没有元数据,因此参数的类型由.Net 框架到 SQL Server 的映射规则决定- 并且字符串被映射到nvarchar
.
因此,不要使用AddWithValue
,而是使用Add
:
comm.Parameters.Add("@date", SqlDbType.Date).Value = textBox_livr.Text;
comm.Parameters.Add("@pu", SqlDbType.Int).Value = textBox_prix.Text;
comm.Parameters.Add("@code", SqlDbType.Int).Value = textBox_art.Text;
comm.Parameters.Add("@remise", SqlDbType.Int).Value = textBox_rem.Text;
请注意,我已经使用SqlDbType.Date
并SqlDbType.Int
作为默认值,因为我不知道您在表中使用的实际数据类型 - 很像 c# 编译器,我无法读入您的数据库,所以我必须猜测。
有关更多信息,请阅读 Joel Coehoorn的我们可以停止使用 AddWithValue() 吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句