2017-02-18 140 views
-1

我正在使用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 
+0

此代码以何种方式失败?看起来你正在执行'INSERT'语句,是不是你想要做的? – David

+0

感谢您的回复先生,只有一条记录插入到datagridview的数据库,而不是多条记录.... –

+0

当您在调试器中逐步完成此操作时,行为与您的期望有何不同?你有一些条件和循环,或许变量的状态不是你所假设的。当你检查时,会发生什么? – David

回答

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 
+0

感谢您的回复先生,正确的,即时将要插入多个记录一次,并将插入(所有记录,可能超过5记录)在选定的行...你能写基本上从我的代码示例?以前感谢... –

+0

当然,我刚刚编辑了这篇文章,以便您可以使用它开始。 –

+0

为什么downvote,任何人? –