必须声明标量变量"@uom"
本文关键字:quot @uom 变量 声明 标量 | 更新日期: 2023-09-27 18:13:32
我有一个Windows应用程序,我应该填写采购订单表单。它有一个叫做uom的域。现在我使用参数化查询插入。但是我得到了这个错误
必须声明标量变量@uom。
请帮助。
private void btnsave_Click(object sender, EventArgs e)
{
con.Open();
if (IsAllValid())
{
cmd.CommandText = "insert into purchaseorder(req_no,purchaseorder_no,purchaseorder_date,costcenter_id,vendor_id,payment_terms,delivery_schedule,emp_id,commercial_name) values(@req_no,@purchaseorder_no,@purchaseorder_date,@costcenter_id,@vendor_id,@payment_terms,@delivery_schedule,@emp_id,@commercial_name)";
cmd.Parameters.AddWithValue("@req_no", cmbreqno.SelectedValue);
cmd.Parameters.AddWithValue("@purchaseorder_no", txtpno.Text);
cmd.Parameters.AddWithValue("@purchaseorder_date", dtpo.Value);
cmd.Parameters.AddWithValue("@costcenter_id", Convert.ToString(txtcc.Tag));
cmd.Parameters.AddWithValue("@vendor_id", cmbvendorname.SelectedValue);
cmd.Parameters.AddWithValue("@payment_terms", txtpaymentterms.Text);
cmd.Parameters.AddWithValue("@delivery_schedule", txtdeliveryschedule.Text);
cmd.Parameters.AddWithValue("@emp_id", cmbcontactperson.SelectedValue);
cmd.Parameters.AddWithValue("@commercial_name", txtcommercial.Text);
cmd.ExecuteScalar();
foreach (var item in poitemlist)
{
cmd.CommandText = "insert into purchaseorderitem(uom,item_id,quantity,item_cost,total_cost)values(@uom,@item_id,@quantity,@item_cost,@total_cost)";
cmd.Parameters.AddWithValue("@item_id", item.Item_ID.ToString());
cmd.Parameters.AddWithValue("@uom", item.uom.ToString());
cmd.Parameters.AddWithValue("@quantity", item.Quantity.ToString());
cmd.Parameters.AddWithValue("@item_cost", item.price.ToString());
cmd.Parameters.AddWithValue("@total_cost", item.TotalCost.ToString());
cmd.Parameters.Clear();
cmd.ExecuteScalar();
}
MessageBox.Show("Purchase Order saved", "Purchase Order", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("error", "Purchase Order", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
con.Close();
}
将用于清除参数集合的行移到ExecuteScalar
之后 cmd.ExecuteScalar();
cmd.Parameters.Clear();
顺便说一下,如果您有许多项要插入,最好使用StoredProcedure,或者进行一些优化,将参数的创建移出循环并仅在循环内设置
cmd.Parameters.Clear();
cmd.CommandText = "insert into purchaseorderitem(uom,item_id,quantity,item_cost,total_cost)" +
"values(@uom,@item_id,@quantity,@item_cost,@total_cost)";
// Create the parameters collection with fake values
// It is better to use the Add with the specific datatype,
// but for this example I will use AddWithValue
cmd.Parameters.AddWithValue("@item_id", string.Empty);
cmd.Parameters.AddWithValue("@uom", string.Empty);
cmd.Parameters.AddWithValue("@quantity", string.Empty);
cmd.Parameters.AddWithValue("@item_cost", string.Empty);
cmd.Parameters.AddWithValue("@total_cost", string.Empty);
foreach (var item in poitemlist)
{
cmd.Parameters["@item_id"].Value =, item.Item_ID.ToString();
cmd.Parameters["@uom"].Value = item.uom.ToString();
cmd.Parameters["@quantity"].Value = item.Quantity.ToString();
cmd.Parameters["@item_cost"].Value = item.price.ToString();
cmd.Parameters["@total_cost"].Value = item.TotalCost.ToString();
cmd.ExecuteScalar();
}
删除这一行
cmd.Parameters.Clear();
从foreach (var item in poitemlist)
{
cmd.CommandText = "insert into purchaseorderitem(uom,item_id,quantity,item_cost,total_cost)values(@uom,@item_id,@quantity,@item_cost,@total_cost)";
cmd.Parameters.AddWithValue("@item_id", item.Item_ID.ToString());
cmd.Parameters.AddWithValue("@uom", item.uom.ToString());
cmd.Parameters.AddWithValue("@quantity", item.Quantity.ToString());
cmd.Parameters.AddWithValue("@item_cost", item.price.ToString());
cmd.Parameters.AddWithValue("@total_cost", item.TotalCost.ToString());
cmd.Parameters.Clear();
cmd.ExecuteScalar();
}