我想根据数据库中的折扣修改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
答案 0 :(得分:1)
正如戈登和斯图尔特在评论中所写,问题在于您使用的是AddWithValue
。
使用AddWithValue
时,编译器必须从值(和元数据,如果存在)推断参数的数据类型。
如果您使用的是内联sql,则没有元数据,因此参数的类型由.Net framework to SQL Server mapping rules决定 - 字符串映射到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的Can we stop using AddWithValue() already?