【发布时间】:2021-08-05 22:27:51
【问题描述】:
Query = "update Product set ProductName=@ProductName,Specification=@Specification" +
",DepartmentId=@DepartmentId,UnitId=@UnitId,TCategoryId=@TCategoryId,HSNId=@HSNId,SGroupId=@SGroupId" + " where ProductCode=@ProductCode";
using (SqlCeConnection con = new SqlCeConnection(strConnection))
{
using (SqlCeCommand cmd = new SqlCeCommand(Query, con))
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@ProductCode", dgvAdjustment.Rows[i].Cells[0].Value.ToString().ToUpper());
cmd.Parameters.AddWithValue("@ProductName", dgvAdjustment.Rows[i].Cells[1].Value.ToString().ToUpper());
cmd.Parameters.AddWithValue("@Specification", strSpecification);
cmd.Parameters.AddWithValue("@DepartmentId", strDepartmentId);
cmd.Parameters.AddWithValue("@UnitId", strUnitId);
cmd.Parameters.AddWithValue("@TCategoryId", strTCategoryId);
cmd.Parameters.AddWithValue("@HSNId", strHSNId);
cmd.Parameters.AddWithValue("@SGroupId", strSGroupId);
con.Open();
cmd.ExecuteNonQuery();
}
}
【问题讨论】:
-
您需要传递
DBNull.Value而不是null,因此对于每个参数,您可以执行strSGroupId ?? (object) DBNull.Value之类的操作。而你shouldn't useAddWIthValueanyway,明确指定你的参数类型和长度。另外cmd.Parameters.Clear();是不必要的,它是一个新的命令对象。 -
请添加正确的解释和代码 sn-ps 或您面临的错误。
-
@Charlieface 不起作用
-
@PrathameshMore 如上所述,先生
-
@Charlieface 先生,虽然它通过了错误,但它已成功更新