将dataGridview中的多行插入Microsoft Access中的单行

时间:2019-03-27 15:53:16

标签: vb.net ms-access datagridviewcolumn

我试图将datagridView的多行插入Microsoft Access表中的单行。该应用程序是销售点系统。

我已经能够将datagrid中的多行添加到数据库表中,但是它将它们分别保存在表中。我希望将其保存在数据库表的单行中。

Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click
    Try
        Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\inventory1.accdb")
            cn.Open()
            For Each row As DataGridViewRow In dgv_order_cart.Rows
                Using cmd2 As New OleDbCommand("INSERT INTO tblTransaction (Product_Name,Price,Qty,Amount) VALUES (@Product_Name,@Price,@Qty,@Amount)", cn)
                    cmd2.Parameters.AddWithValue("@Product_Name", OleDbType.VarChar)
                    cmd2.Parameters.AddWithValue("@Price", OleDbType.Currency)
                    cmd2.Parameters.AddWithValue("@Qty", OleDbType.Decimal)
                    cmd2.Parameters.AddWithValue("@Amount", OleDbType.Currency)

                    With cmd2
                        .Parameters("@Product_Name").Value = row.Cells(0).Value
                        .Parameters("@Price").Value = row.Cells(1).Value
                        .Parameters("@Qty").Value = row.Cells(2).Value
                        .Parameters("@Amount").Value = row.Cells(3).Value
                    End With

                    cmd2.ExecuteNonQuery()
                End Using
            Next
            'End Using 'You probably want to catch more specific exceptions
            'and handle them separately
        End Using
    Catch ex As Exception 'You probably want to catch more specific exceptions
        'and handle them separately
        'MessageBox.Show(ex.Message)
    End Try
    MessageBox.Show("TRANSACTION INSERTED SUCCESFULLY")
End Sub

我希望将datagridview中的多个记录保存在数据库表的一行中。

0 个答案:

没有答案