将 2 插入到一个命令中
Combining 2 insert into in one command
我想不通:
我想在 table-a 中添加一行,此行的 3 列将来自 table-b,其他 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);
只需将参数作为静态值放在 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);
当你在做的时候,参数化 where 子句:
我想不通: 我想在 table-a 中添加一行,此行的 3 列将来自 table-b,其他 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);
只需将参数作为静态值放在 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);
当你在做的时候,参数化 where 子句: