我如何通过从另一个表中为特定字段选择数据来插入表中

时间:2018-10-27 09:21:31

标签: vb.net oledb

我正在使用OLEDB连接到Microsoft Access数据库。我想从“ tblItemMst”中将“ ItemDesc”字段数据插入“ tblCallibrationNewGauge”中,并从用户那里获取另一个字段,例如“ ItemCode”,“ Customer”,“ Quantity”值。下面是我的代码:

 If con.State = ConnectionState.Open Then
  con.Close()
  con.Open()
  Else
 con.Open()
 End If
  Dim cmd As New OleDbCommand("Insert Into tblCallibrationNewGauge(ItemCode,ItemDesc,Customer,Quantity)VALUES('" + partCode.Text.ToString() + "',(Select ItemDesc from tblItemMst where ItemCode='" + partCode.Text.ToString() + "'),'" + cmb_customer.Text.ToString() + "','" + quantity.Text.ToString() + "'",con)
If cmd.ExecuteNonQuery() Then
 MessageBox.Show("Data Inserted")
End If
con.Close()

我为此收到异常错误。 有可能这样做吗?要么 有什么方法可以将数据插入表中,其中某些字段取自其他表,而某些字段取自winform Control?

谢谢。

1 个答案:

答案 0 :(得分:0)

更改命令文本,如下所示:

Dim cmd As New OleDbCommand("Insert Into tblCallibrationNewGauge( ItemCode,ItemDesc,Customer,Quantity) SELECT '" + partCode.Text.ToString() + "', ItemDesc,'" + cmb_customer.Text.ToString() + "','" + quantity.Text.ToString() + "' from tblItemMst where ItemCode='" + partCode.Text.ToString() + "'",con)
相关问题