我尝试更新
Company_name varchar(40) not null,
Product_unit varchar(20) not null,
Product_quantity int not null,
Product_Barcode varchar(100) null,
Product_expire_date datetime not null,
DurationBeforeWarningsInDays int not null,
DecreasingAmountBeforeWarningsInUnit int not null,
Product_Purchase_price float not null,
Product_Price float not null
在我的数据库中的产品表中并从 datagridview 单元格中获取字段值并将其转换为数据库中的等效项,但我发现了这个异常
(无法绑定多部分标识符“m.Medicine_contraindications”。)
当我的程序执行cmd.ExecuteNonQuery();
语句和我不知道这个原因
private void Update_Click(object sender, EventArgs e)
{
string strconn = @"Server=.\SQLEXPRESS;initial catalog=PharmacyV2;integrated security=true;";
SqlConnection conn = new SqlConnection(strconn);
string mysql = " UPDATE p set p.Company_name=@C_name,p.Product_unit=@unit,p.Product_quantity=@quantity,p.Product_Barcode=@Barcode,p.DecreasingAmountBeforeWarningsInUnit=@warningAmount,p.Product_expire_date=@EDate,p.DurationBeforeWarningsInDays=@warningTime,p.Product_Purchase_price=@P_price,p.Product_Price=@price ,m.Medicine_contraindications=@M_contr ,m.Medicine_sideEffects=@M_sid from Product p join Medicine m on p.Product_id=m.Product_id where p.Product_name=@name;";
SqlCommand cmd = new SqlCommand(mysql, conn);
conn.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
cmd.Parameters.Add("@name", SqlDbType.NVarChar);
cmd.Parameters["@name"].Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
cmd.Parameters.Add("@C_name", SqlDbType.NVarChar);
cmd.Parameters["@C_name"].Value = dataGridView1.Rows[i].Cells[1].Value.ToString();
cmd.Parameters.Add("@unit", SqlDbType.NVarChar);
cmd.Parameters["@unit"].Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
cmd.Parameters.Add("@quantity", SqlDbType.Int);
cmd.Parameters["@quantity"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
cmd.Parameters.Add("@Barcode", SqlDbType.NVarChar);
cmd.Parameters["@Barcode"].Value = dataGridView1.Rows[i].Cells[4].Value.ToString();
cmd.Parameters.Add("@warningAmount", SqlDbType.Int);
cmd.Parameters["@warningAmount"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value);
cmd.Parameters.Add("@EDate", SqlDbType.DateTime);
cmd.Parameters["@EDate"].Value = Convert.ToDateTime(dataGridView1.Rows[i].Cells[6].Value);
cmd.Parameters.Add("@warningTime", SqlDbType.Int);
cmd.Parameters["@warningTime"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells[7].Value);
cmd.Parameters.Add("@P_price", SqlDbType.Decimal);
cmd.Parameters["@P_price"].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[8].Value);
cmd.Parameters.Add("@price", SqlDbType.Decimal);
cmd.Parameters["@price"].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[9].Value);
ArrayList arr = new ArrayList();
arr.Add(TextBox1.Text);
ArrayList arr1 = new ArrayList();
arr1.Add(TextBox2.Text);
cmd.Parameters.AddWithValue("@M_contr",Convert.ToString(arr1));
cmd.Parameters.AddWithValue("@M_sid", Convert.ToString(arr));
cmd.ExecuteNonQuery();