我正在创建用于将数据从vb.net中的datagridview插入到数据库表Access 2007的代码。但是这个程序是为了插入来自用户的数据,当他在第一行的datagridview中输入数据,然后在下一次(在break by break之后)他在第二,第三下一行中再次输入数据,并在我的项目中单击保存按钮在数据库中插入数据。但问题出现了,当他点击保存第二次插入数据到数据库的过程代码插入所有行旧和新的再次。插入不正确。它重复数据。所以请建议我使用dt for datatable和sql for 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中有额外的行,所以它的代码是rows-2
发布于 2020-08-19 15:49:40
您似乎没有在insert之后清除变量'strInsert‘,所以这可能是您面临的问题?
我很想在MsgBox之后清除这个变量。
非常感谢POC
发布于 2020-08-19 23:03:36
这里有几个关于你的代码片段的问题。
单击Dim sql = "SELECT * FROM RABill“maxrow = get_maxrow(sql) For i=0 To DgRAbill.Rows.Count -2\f2 Try Dim strInsert As Button5_Click= "INSERT INTO RABill (Billnumber,RDate,RAmount,Below,Royalti,SD,IT,GSTminus,RAccountname,GstTwelvepercent,保险,其他,TotalBill,备注)值('“_& 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 =新建OleDbCommand(strInsert,conn) cmd.ExecuteNonQuery() MsgBox(“成功保存记录”) DgRabill.Rows.Clear() Catch ex As Exception MessageBox.Show(“在行插入记录时出错”&i& vbCrLf & ex.Message) End Try Next End Sub
发布于 2020-08-25 17:03:00
您可以使用以下代码将DataGridView数据绑定并保存到数据库。
Dim connString As String = ""
Private ds As DataSet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ds = New DataSet()
Using con As SqlConnection = New SqlConnection(connString)
con.Open()
Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from RABill", con)
adp.Fill(ds)
Dim dt As DataTable = ds.Tables(0)
DataGridView1.DataSource = dt
End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using con As SqlConnection = New SqlConnection(connString)
con.Open()
Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from RABill", con)
Dim build As SqlCommandBuilder = New SqlCommandBuilder(adp)
adp.Update(ds.Tables(0))
End Using
End Sub无论您在DataGridView中编辑什么,请单击button1,这些记录将保存在数据库中。
https://stackoverflow.com/questions/63481352
复制相似问题