使用VB.Net将事务记录插入MsAccess

时间:2016-07-09 15:16:59

标签: vb.net ms-access

我正在尝试将多条记录插入到Access数据库中。目前我可以使用下面的代码插入单个交易记录

Try
    sql = "INSERT INTO  tblINV_SalesRecord " &_ 
          "(transID, itemcode, itemname, itemunits ) " &_ 
          "VALUES ('TGR011111','Cheese','INV0234','5')"

    Using con1 As New OleDbConnection(dbProvider & dbSource)
       Dim command As New OleDbCommand(Sql, con1)
       con1.Open()
       command.ExecuteScalar()
        MsgBox("Record Inserted")
    End Using
Catch ex As Exception
      MsgBox("An Error Occured")
      MessageBox.Show(ex.Message & " - " & ex.Source)
End Try

现在我要做的是在一个SQL语句中插入客户购买的多个项目。我的代码如下所示。

Try
    sql = "INSERT INTO  tblINV_SalesRecord " &_ 
          "(transID, itemcode, itemname, itemunits ) " &_ 
          "VALUES ('TGR011111','Cheese','INV0234','5')," &_ 
                 "('TGR011111','Cake','INV0114','2')," &_ 
                 "('TGR011111','Burger','INV0217','3')" &_ 

    Using con1 As New OleDbConnection(dbProvider & dbSource)
       Dim command As New OleDbCommand(Sql, con1)
       con1.Open()
       command.ExecuteScalar()
        MsgBox("Record Inserted")
    End Using
Catch ex As Exception
      MsgBox("An Error Occured")
      MessageBox.Show(ex.Message & " - " & ex.Source)
End Try

但它返回错误。请问我如何解决这个问题?

0 个答案:

没有答案