我正在使用Visual Basic(VB 2010)。 如何使用MS Access 2007將多條記錄插入數據庫?我的代碼不起作用:使用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
此代碼以何種方式失敗?看起來你正在執行'INSERT'語句,是不是你想要做的? – David
感謝您的回覆先生,只有一條記錄插入到datagridview的數據庫,而不是多條記錄.... –
當您在調試器中逐步完成此操作時,行爲與您的期望有何不同?你有一些條件和循環,或許變量的狀態不是你所假設的。當你檢查時,會發生什麼? – David