如何解决如何逐步从vb.net中的datagridview保存插入数据表中的数据
我正在创建用于将数据从vb.net中的datagridview插入数据库表Access 2007的代码。但是该程序用于在用户在第一行中的datagridview中输入数据时插入用户的数据,然后在下一次之后(逐断)他在第二行,第二行和第三行再次输入数据,然后单击我项目中的保存按钮以将数据插入数据库。 但是,当他第二次单击“保存”以将数据插入数据库时,就会出现问题,过程代码再次将所有行插入新旧行。这是不正确的插入。它重复数据。 所以请建议我 我将dt用于数据表,将sql用于sql语句。
Private Sub Button5_Click(sender As Object,e As EventArgs) Handles Button5.Click
Dim sql = "SELECT * FROM RABill "
maxrow = get_maxrow(sql)
For i = 0 To DgRAbill.Rows.Count - 2
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM RABill ")
Dim strInsert As String = "INSERT INTO RABill (Billnumber,RDate,RAmount,Below,Royalti,SD,IT,GSTminus,RAccountname,GstTwelvepercent,Insurance,Other,TotalBill,Remark) VALUES ('" _
& CStr(DgRAbill.Rows(i).Cells(1).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(2).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(3).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(4).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(5).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(6).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(7).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(8).FormattedValue) & "','" _
& ComboBox1.Text & "','" _
& CStr(DgRAbill.Rows(i).Cells(10).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(11).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(12).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(13).FormattedValue) & "','" _
& CStr(DgRAbill.Rows(i).Cells(14).FormattedValue) & "')"
cmd = New OleDbCommand(strInsert,conn)
cmd.ExecuteNonQuery()
MsgBox("Record Saved Successfully")
Next
End Sub
在上面的代码中,我的datagridview中有多余的一行,因此代码为2行
解决方法
插入后似乎没有清除变量'strInsert',所以这可能是您面临的问题?
我很想在MsgBox之后清除该变量。
非常感谢 POC
,此处与您的代码段有关的几件事。
-
您的数据库是否应该能够有重复的条目?您可能想看看如何设置主键。
-
您确实应该在这里捕获一些错误,尤其是在数据库连接中。浏览一下以供参考:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement
-
对于实际的重复输入-保存所有记录后,您不会清除datagridview。如果我正确理解用户在DGV中填写的一堆行,然后单击“保存”,它就会循环遍历并将其全部插入。然后,他继续输入并再次点击保存,然后将其全部插入(有些重复)。保存后,应在DGV上执行.clear清除-或需要创建一种在两次保存之间持久存在的计数器,这是在“保存”按钮中开始for循环的地方。每次保存后,下面将其清除:>
Private Sub Button5_Click(sender As Object,e As EventArgs) Handles Button5.Click Dim sql = "SELECT * FROM RABill " maxrow = get_maxrow(sql) For i = 0 To DgRAbill.Rows.Count - 2 Try Dim strInsert As String = "INSERT INTO RABill (Billnumber,RDate,RAmount,Below,Royalti,SD,IT,GSTminus,RAccountname,GstTwelvepercent,Insurance,Other,TotalBill,Remark) VALUES ('" _ & CStr(DgRAbill.Rows(i).Cells(1).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(2).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(3).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(4).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(5).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(6).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(7).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(8).FormattedValue) & "','" _ & ComboBox1.Text & "','" _ & CStr(DgRAbill.Rows(i).Cells(10).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(11).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(12).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(13).FormattedValue) & "','" _ & CStr(DgRAbill.Rows(i).Cells(14).FormattedValue) & "')" cmd = New OleDbCommand(strInsert,conn) cmd.ExecuteNonQuery() MsgBox("Record Saved Successfully") DgRabill.Rows.Clear() Catch ex As Exception MessageBox.Show("Error Inserting record at row " & i & vbCrLf & ex.Message) End Try Next End Sub
您可以使用以下代码将DataGridView数据绑定并保存到数据库中。
document.all
无论您在DataGridView中进行什么编辑,请单击button1那些记录都将保存在数据库中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。