使用MS Access 2007将多个记录插入数据库

时间:2017-02-18 03:26:12

标签: vb.net

我正在使用Visual Basic(VB 2010)。 如何使用MS Access 2007将多个记录插入数据库?我的代码不起作用:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If DataGridView1.Rows(0).Cells(0).Value = "" Then
            MsgBox("Belum ada transaksi", MsgBoxStyle.Exclamation, "Informasi")
            Exit Sub
        End If
        If TextBox6.Text = "" Then
            MsgBox("Jumlah bayar belum diinput!", MsgBoxStyle.Exclamation, "Informasi")
            Exit Sub
        End If
        On Error Resume Next
        If RadioButton1.Checked Then
            For baris As Integer = 0 To DataGridView1.Rows.Count - 2
                Dim simpan As String = "Insert into TBL_JUALTUNAI (NomorFaktur,TglTransaksi,WaktuTransaksi,KodeBarang,NamaBarang,HargaSatuan,JumlahBeli,Total) values " & _
        "('" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & DataGridView1.Rows(0).Cells(0).Value & "','" & DataGridView1.Rows(0).Cells(1).Value & "','" & DataGridView1.Rows(0).Cells(2).Value & "','" & DataGridView1.Rows(0).Cells(3).Value & "','" & DataGridView1.Rows(0).Cells(4).Value & "')"
                CMD = New OleDbCommand(simpan, CONN)
                CMD.ExecuteNonQuery()
            Next baris
        End If

        If RadioButton2.Checked Then
            Dim simpan1 As String = "Insert into TBL_PELANGGAN (NomorFaktur,TglTransaksi,WaktuTransaksi,NamaPelanggan,AlamatPelanggan,TelpPelanggan,KodeBarang,NamaBarang,HargaSatuan,JumlahBeli,Total) values " & _
"('" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & DataGridView1.Rows(0).Cells(7).Value & "','" & DataGridView1.Rows(0).Cells(8).Value & "','" & DataGridView1.Rows(0).Cells(9).Value & "','" & DataGridView1.Rows(0).Cells(0).Value & "','" & DataGridView1.Rows(0).Cells(1).Value & "','" & DataGridView1.Rows(0).Cells(2).Value & "','" & DataGridView1.Rows(0).Cells(3).Value & "','" & DataGridView1.Rows(0).Cells(4).Value & "')"
            CMD = New OleDbCommand(simpan1, CONN)
            CMD.ExecuteNonQuery()
        End If

        CMD = New OleDbCommand("select * from TBL_BARANG where KodeBarang='" & DataGridView1.Rows(0).Cells(0).Value & "'", CONN)
        RD = CMD.ExecuteReader
        RD.Read()
        If RD.HasRows Then
            Dim kurangistok As String = "update TBL_BARANG set StockBarang= '" & RD.Item(4) - DataGridView1.Rows(0).Cells(3).Value & "' where KodeBarang='" & DataGridView1.Rows(0).Cells(0).Value & "'"
            CMD = New OleDbCommand(kurangistok, CONN)
            CMD.ExecuteNonQuery()
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

使用该代码,您不会一次插入多个记录。每个INSERT命令只会插入一行,因此您必须对多行使用多个INSERT命令。

如果我正确理解你,你有一个DataGridView,其中有多行,你想要插入选中的行,是吗? 如果我错了,请纠正我,但你应该做的是通过DataGridView的行枚举并为该DataGridView中的每一行向数据库插入一行?

在这种情况下,使用For Each循环检索行(DataGridView1.Rows),然后在该循​​环中执行INSERT命令。

编辑:在回复您的评论时,我不知道您的代码的完整背景,但我可以帮助您入门。

    For Each dgvRow As DataGridViewRow In dgv.Rows
        Dim myCommand As String = "INSERT INTO " 'From here, insert your parameters for that row. You can call on dgvRow.Cells.
        CMD = New OleDbCommand(myCommand, CONN)
        CMD.ExecuteNonQuery()
    Next