2016-04-28 81 views
1

我無法弄清楚: 我想在表中添加一行a,此行的3列將來自表b,其他2列將來自例如文本框... 此代碼沒有工作......將2插入到一個命令中

SqlCommand cmd35 = new SqlCommand("INSERT INTO BTmr (Barcode,[Machine Name],[Machine ID]) SELECT Barcode,[Machine Name],[Machine ID] FROM BkmP WHERE barcode like '" + c13 + "%' UNION INSERT INTO BTmr([Repair Cost],[Repair Date],Barcode)values (@cst,@rprd)", connection); 

      cmd35.Parameters.AddWithValue("@cst", textBox10.Text); 
      cmd35.Parameters.AddWithValue("@rprd", dateTimePicker1.Text); 
+0

選擇表-B的列投入表之前和你將所有的值成爲表 - 輸入一個。 – randominstanceOfLivingThing

+0

您的選擇應該在insert語句之外執行,以便將列值讀入變量。 – randominstanceOfLivingThing

回答

0

只要把參數靜態值在SELECT語句。

SqlCommand cmd35 = new SqlCommand("INSERT INTO BTmr (Barcode,[Machine Name], 
    [Machine ID],[Repair Cost],[Repair Date]) SELECT Barcode,[Machine Name], 
    [Machine ID],@cst AS [Repair Cost],@rprd AS [Repair Date] FROM BkmP WHERE 
    barcode like '" + c13 + "%', connection); 

cmd35.Parameters.AddWithValue("@cst", textBox10.Text); 
cmd35.Parameters.AddWithValue("@rprd", dateTimePicker1.Text); 

雖然your're它,那參數where子句:https://stackoverflow.com/a/251380/123422

+0

非常感謝你的工作...... –