2013-11-02 49 views
1

我嘗試更新數據庫更新表例外

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 [email protected]_name,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]_price,[email protected] ,[email protected]_contr ,[email protected]_sid from Product p join Medicine m on p.Product_id=m.Product_id where [email protected];"; 

     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(); 
+0

你爲什麼要更新連接? – neeKo

+0

,因爲我需要從產品表中的Product_name字段更新Medicine表中的2個字段 –

+0

您如何在此處單獨更新? – neeKo

回答

0

你要更新一個更新語句兩個表,你需要把它分解成two update statements,並將這兩個更新語句one Transaction,這樣你就可以回滾整個交易如果出現錯誤,那麼您的數據不會處於半完成狀態。你可以做這樣的事情

BEGIN TRANSACTION 

UPDATE p 
      set [email protected]_name, 
       [email protected], 
       [email protected], 
       [email protected], 
       [email protected], 
       [email protected], 
       [email protected], 
       [email protected]_price, 
       [email protected] 

from Product p join Medicine m 
on p.Product_id=m.Product_id 
where [email protected]; 



UPDATE m 
      set [email protected]_contr , 
       [email protected]_sid 
from Product p join Medicine m 
on p.Product_id=m.Product_id 
where [email protected]; 

COMMIT TRANSACTION